5

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 pulling any records though the same works in mongoshell.

I tried using require('mongodb').Long with query as
{$gte: Long.fromString("635186135151387725",10)} but didnot work.

Also tried modules "node-int64","int64-native" but no luck.

Is there a Node Module to rescue ?

1 Answer 1

12

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 } } ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.