2014-02-19 88 views
3

我已存儲蒙戈集合中的日期字段作爲NumberLong節點JS的MongoDB查詢NumberLong

節點查詢關於此集合的日期字段{$ GTE:NumberLong(「635186135151387725」)}是 不拉任何記錄,雖然在mongoshell中也是一樣的。

我嘗試使用要求( 'mongodb的')長與查詢作爲
。{$ GTE:Long.fromString( 「635186135151387725」,10)},但因此未工作。

也試過模塊「node-int64」,「int64-native」但沒有運氣。

是否有節點模塊進行救援?

回答

12

這對我來說很好,也許你的查詢沒有正確發出。請看下面的數據和代碼作爲一個例子來比較:

> 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 } } ]