Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js Mongodb Query NumberLong

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 ?

like image 289
rakeshvenkata Avatar asked Feb 19 '14 02:02

rakeshvenkata


1 Answers

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 } } ]
like image 177
Neil Lunn Avatar answered Oct 13 '22 00:10

Neil Lunn