开发者

Timestamp mongodb using node.js

开发者 https://www.devze.com 2023-04-12 23:27 出处:网络
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:

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) {
      开发者_C百科  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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消