Search code examples
mongodbangularmeteorangular-meteor

Difference in _id values in mongo, and how to handle them (angular 2 meteor tutorial)


I'm doing the tutorial at https://angular-meteor.com/tutorials/socially/angular2/routing-and-multiple-views and realise some of my route links were not working.

Initially I thought this was the solution (Meteor JS Routing on Angular 2 Tutorial doesn't work), converting them into strings but that didnt work either. i dug a little deeper and realised this: the route links works for data inserted through the app but not for data inserted through the console as they had _id fields with an ObjectId in it.

{ "_id" : ObjectId("5859f7d5a8aeb662e0e3eff9"), "name" : "A new party", 
  "description" : "From the mongo console!", "location" : "In the DB" }

vs

{ "_id" : "67dswyFrT3Bt3kHED", "name" : "inserted works", 
  "description" : "inserted works", "location" : "inserted works" }

How can I handle both kind of _id data?


Solution

  • There is an explanation of why the two are different: Meteor.Collection.ObjectID() vs MongoDB ObjectId()

    Basically Meteor can use Mongo ObjectId's, but it uses simpler strings because it makes it easier to put _id fields in URL's etc

    Normally only your app will ever modify the data, so it shouldn't be a problem.