最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Timestamp mongodb using node.js - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Oct 13, 2011 at 13:14 rissicayrissicay 4054 silver badges14 bronze badges 2
  • 1 couldn't you just use type Date and something like Date.now()? Does it have to be a Timestamp? – Philipp Kyeck Commented Oct 13, 2011 at 14:59
  • yeaa, if no one knows the fault that what ill end up doin. It just seems more right to make mongodb fill in the timestamp – rissicay Commented Oct 13, 2011 at 16:29
Add a ment  | 

2 Answers 2

Reset to default 3

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/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./christkv/node-mongodb-native/blob/master/lib/mongodb/bson/timestamp.js#L41 for more.

发布评论

评论列表(0)

  1. 暂无评论