15

If you create a Mongo document directly inside Mongo and want to access this same document via Meteor, what is the best way to accomplish this task?

I am getting undefined result when I attempt to access.

If you create a new document from Meteor it does not prefix the id with ObjectId("").

Any help would be greatly appreciated.

I want to simply find exact document by exact ObjectId.

jremi
  • 2,879
  • 3
  • 26
  • 33

1 Answers1

30

Use Meteor.Collection.ObjectID:

var oid = new Meteor.Collection.ObjectID("a86ce44f9a46b99bca1be7a9");
var doc = SomeCollection.findOne(oid);

See the options for how unique IDs in collections are generated. However, it's general practice in Meteor to use the string approach because clients can then generate unique IDs reliably.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
  • Thanks! This worked great. I did not realize that I needed to create a new local collection to push the Mongo ObjectID into before I can lookup against other collections. – jremi Jul 01 '14 at 18:45
  • @jremi not sure what you mean by that. – Andrew Mao Jul 01 '14 at 18:57
  • I did not realize that I needed to create a new variable to associate the ObjectID before passing to the find call. I was trying to pass the ObjectId directly into the findOne and was having issues. Now things are working. – jremi Jul 01 '14 at 19:22
  • @jremi: you don't need a variable, you can just pass it as `SomeCollection.findOne(new Meteor.Collection.ObjectID("a86ce44f9a46b99bca1be7a9"));`. The key here is to create `Meteor.Collection.ObjectID` object. – Hubert OG Jul 02 '14 at 06:53