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.

2
  • 1
    couldn't you just use type Date and something like Date.now()? Does it have to be a Timestamp? 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 Commented Oct 13, 2011 at 16:29

2 Answers 2

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.org/display/DOCS/Dates

ISODate() is the correct type to use.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.