gpt4 book ai didi

javascript - Node js Mongodb查询NumberLong

转载 作者:可可西里 更新时间:2023-11-01 09:08:01 28 4
gpt4 key购买 nike

我有一个日期字段作为 NumberLong 存储在 mongo 集合中

此集合上日期字段 {$gte: NumberLong("635186135151387725")} 的 Node 查询是尽管在 mongoshell 中同样有效,但没有提取任何记录。

我尝试使用 require('mongodb').Long 和查询作为
{$gte: Long.fromString("635186135151387725",10)} 但没有用。

还尝试了模块“node-int64”、“int64-native”,但没有成功。

是否有 Node 模块可以拯救?

最佳答案

这对我来说很好,也许你的查询没有正确发出。以下面的数据和代码为例进行比较:

> db.test.find()
{
"_id" : ObjectId("5303f24423d2721c25c493ee"),
"ts" : NumberLong("635186135151387725")
}
{
"_id" : ObjectId("5303f24a23d2721c25c493ef"),
"ts" : NumberLong("635186135151387726")
}
>

以及要查找的代码:

var MongoClient = require('mongodb').MongoClient;

var Long = require('mongodb').Long;

MongoClient.connect('mongodb://localhost/test', function(err, db) {

var collection = db.collection('test');

var value = Long.fromString("635186135151387726");

console.log( value );

var cursor = collection.find({ ts: {"$gte": value} });

cursor.toArray(function(err, items) {
console.log( items );
});

});

按预期提供输出:

{ _bsontype: 'Long', low_: -1342987186, high_: 147890796 }
[ { _id: 5303f24a23d2721c25c493ef,
ts: { _bsontype: 'Long', low_: -1342987186, high_: 147890796 } } ]

关于javascript - Node js Mongodb查询NumberLong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21870044/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com