Node js Mongodb Query NumberLong

后端 未结 1 1355
刺人心
刺人心 2021-01-01 05:14

I have a date field stored in mongo collection as NumberLong

Node query on this collection for the date field {$gte: NumberLong(\"635186135151387725\")} is not pull

相关标签:
1条回答
  • 2021-01-01 06:01

    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") 
    }
    >
    

    And the code to find:

    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 );
        });
    
    });
    

    Gives output as expected:

    { _bsontype: 'Long', low_: -1342987186, high_: 147890796 }
    [ { _id: 5303f24a23d2721c25c493ef,
        ts: { _bsontype: 'Long', low_: -1342987186, high_: 147890796 } } ]
    
    0 讨论(0)
提交回复
热议问题