简体   繁体   English

使用node.js的时间戳mongodb

[英]Timestamp mongodb using node.js

i have a mongodb database connected to a node.js app via mongodb-native-drivers. 我有一个通过mongodb-native-drivers连接到node.js应用程序的mongodb数据库。 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! 插入效果很好,但时间戳始终为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": MongoDB文档指出“ Timestamp数据类型,但这是MongoDB的特殊内部类型,通常不应使用”:

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

ISODate() is the correct type to use. ISODate()是要使用的正确类型。

I think value 0 is as per expectation. 我认为值0符合预期。 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 ! 创建对象时,需要提供时间戳的低32位(带符号)和时间戳的高32位(带符号)! Correction would be here. 更正将在这里。 "new db.bson_serializer.Timestamp(someIntLow,someIntHigh)" “新的db.bson_serializer.Timestamp(someIntLow,someIntHigh)”

See https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/bson/timestamp.js#L41 for more. 有关更多信息,请参见https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/bson/timestamp.js#L41

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM