Posts

Showing posts with the label objectid

MongoDB tips : check for types, bi connector issue and resolution

Image
This issue arises when end user mentioned they couldn't locate certain records and they know the record is indeed in the database and they were using _id to reference the record. Quick investigation of our database, we can see our _id type is split into total count: db.getCollection('upDown').find({}).count() //2403170 count by type 2 which is string db.getCollection('upDown').find({_id:{$type:2}}).count() //1630934 count by not type2 db.getCollection('upDown').find({_id:{$not:{$type:2}}}).count() //772236 check a few sample record, we see the _id is in the default format of bson object, objectId db.getCollection('upDown').find({_id:{$not:{$type:2}}}) This issue was first discovered by end user who uses MongoDB  BI connector to query mongodb data. In the drdl I see we have exposed the field as     - Name: _id       MongoType: string       SqlName: _id       SqlType: varchar from MySQL query, it looks lik...

MongoDB Tips - Using _id to determine when the object was inserted

MongoDB objectid conversion to timestamp Basically we can generate timestamp when this document is inserted by converting the objectid to timestamp: Please look at the following for javascript by  Steve Ridout  on his blog https://steveridout.github.io/mongo-object-time/ Why generate an ObjectId from a timestamp? To query documents by creation date. e.g. to find all comments created after 2013-11-01: db.comments.find({_id: {$gt: ObjectId("5272e0f00000000000000000")}}) Javascript functions var objectIdFromDate = function (date) { return Math.floor(date.getTime() / 1000).toString(16) + "0000000000000000"; }; var dateFromObjectId = function (objectId) { return new Date(parseInt(objectId.substring(0, 8), 16) * 1000); };