rissicay
rissicay

Reputation: 405

Timestamp mongodb using node.js

i have a mongodb database connected to a node.js app via mongodb-native-drivers. I am inserting data into the database, and need to timestamp it, the code looks like the following:

var server = new Server('localhost', 27017, { auto_reconnect: true });
var db = new Db('test', server);

exports.fetch = function(args, callback) {
    db.open(function(err, db) {
        db.collection(args['device'], function(err, collection) {
            var doc = {
                          device: args['device'],
                          data: args['data'],
                          time: new db.bson_serializer.Timestamp()
                      }

            collection.insert(doc, { safe: true }, function(err,result) {
                db.close();
                callback(lastestError);
            });
        });
     });
}

The insert goes well, except for the timestamp, which is always 0! I have removed all error checking for clarity and size. Any help would be appreciated! Thanks.

Upvotes: 0

Views: 5074

Answers (2)

cjfont
cjfont

Reputation: 173

The MongoDB documentation states that "Timestamp data type but that is a special internal type for MongoDB that typically should not be used":

http://www.mongodb.org/display/DOCS/Dates

ISODate() is the correct type to use.

Upvotes: 3

Samyak Bhuta
Samyak Bhuta

Reputation: 1254

I think value 0 is as per expectation. You need to provide the low (signed) 32 bits of the Timestamp and the high (signed) 32 bits of the Timestamp values when you create the object ! Correction would be here. "new db.bson_serializer.Timestamp(someIntLow,someIntHigh)"

See https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/bson/timestamp.js#L41 for more.

Upvotes: 2

Related Questions