2 types of implementation of _id of an Object in Mongo

I am following Angular-Meteor Tutorial- Step 3 - 3-Way data binding
.

When i insert through angular; the _id of the Object is as String type but when i insert directly through Mongo shell; the _id of the Object is as ObjectId().

I am using Windows 7, my package versions are
angular@1.2.2
angular-meteor-data@0.0.6
angular:angular@1.4.7
mongo@1.1.3
mongo-id@1.0.1
meteor@1.1.10

{ "_id" : ObjectId("56504c8d62a8031ca2e44d73"), "name" : "ravanan", "description" : "10 heads" }
{ "_id" : "MKQ5CBum5fmnqWSfR", "name" : "raman", "description" : "avatar of vishnu" }

Because of this inconsistency in _id can’t remove or fetch properly using _id.

How to store in string implementation when insert through any interface

check out the docs on mongo collections, perhaps you could try to set the idGeneration field :slight_smile:

also, while less than favorable i have used a transform function in the past to create a standardized ‘id’ field depending on the format the document in question used

is there any global config setting to achieve this

no; i mean you could set a constant for the idGeneration field and reference it whenever you create a new mongo collection, but really in my experience it is best to just use some logic in the transform function and patch _id to a string property (i use id) and then index on that. not ideal and im sure there is a better way…

could you direct me to any sample code to set the constant for idGeneration field or
sample of transform function patch

Something like this…

myCoolCollection = new Mongo.Collection('myCoolCollection', {
    idGeneration: 'MONGO',
     transform: (v) => {
              if(v._id._st) { v.id = v._id._str }
              else { v.id = v._id }
              return v
    }
}
1 Like

Now, this is kinda self-defeating because it will enforce mongo ObjectId generation, then force a string into v.id, however i found this to work quite well when you have data coming from several different mongo databases