【问题标题】:Node js Mongodb Query NumberLongNode js Mongodb 查询 NumberLong
【发布时间】:2014-03-19 04:00:32
【问题描述】:

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

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

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

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

是否有节点模块可以拯救?

【问题讨论】:

    标签: javascript node.js mongodb


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 2019-08-24
      • 2020-09-23
      • 1970-01-01
      相关资源
      最近更新 更多