节点js Mongodb查询号码 [英] Node js Mongodb Query NumberLong
问题描述
我有一个日期字段存储在mongo集合中作为NumberLong
I have a date field stored in mongo collection as NumberLong
此集合上的日期字段的节点查询{$ gte:NumberLong(635186135151387725)}是
虽然在mongoshell中使用相同的工作但没有拉任何记录。
Node query on this collection for the date field {$gte: NumberLong("635186135151387725")} is not pulling any records though the same works in mongoshell.
我尝试使用require('mongodb')。查询为long
{$ gte:Long.fromString(635186135151387725,10)但是没有用。
I tried using require('mongodb').Long with query as
{$gte: Long.fromString("635186135151387725",10)} but didnot work.
还试过模块node-int64,int64-native但没有运气。
Also tried modules "node-int64","int64-native" but no luck.
是否有节点模块需要救援?
Is there a Node Module to rescue ?
推荐答案
这是有效的对我来说很好,也许你的查询没有正确发布。请考虑以下数据和代码作为比较示例:
This works fine for me, perhaps your query is not being issued properly. Consider the following data and code as an example to compare:
> 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 } } ]
这篇关于节点js Mongodb查询号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!