neuront
neuront

Reputation: 9612

mongodb - Construct DBRef with string or ObjectId

I've noticed that either a string or an object id could be used to construct a DBRef in mongodb. For example

db.persons.insert({name: 'alice'})
db.persons.find()
// { "_id" : ObjectId("5165419064fada69cef33ea2"), "name" : "alice" }
db.persons.insert({name: 'bob', sister: new DBRef('persons', '5165419064fada69cef33ea2')}) // use a string
db.persons.find()
// { "_id" : ObjectId("5165419064fada69cef33ea2"), "name" : "alice" }
// { "_id" : ObjectId("516541c064fada69cef33ea3"), "name" : "bob", "sister" : { "$ref" : "persons", "$id" : "5165419064fada69cef33ea2" } }
db.persons.insert({name: 'cavin', sister: new DBRef('persons', new ObjectId('5165419064fada69cef33ea2'))}) // use an ObjectId
db.persons.find()
// { "_id" : ObjectId("5165419064fada69cef33ea2"), "name" : "alice" }
// { "_id" : ObjectId("516541c064fada69cef33ea3"), "name" : "bob", "sister" : { "$ref" : "persons", "$id" : "5165419064fada69cef33ea2" } }
// { "_id" : ObjectId("516541e464fada69cef33ea4"), "name" : "cavin", "sister" : { "$ref" : "persons", "$id" : ObjectId("5165419064fada69cef33ea2") } }

Could anybody tell me what's the difference and which way is preferred?

Upvotes: 4

Views: 11456

Answers (2)

astarring
astarring

Reputation: 154

ObjectId Pros

  • it has an embedded timestamp in it.

  • it's the default Mongo _id type; ubiquitous

  • interoperability with other apps and drivers

ObjectId Cons

  • it's an object, and a little more difficult to manipulate in practice.

  • there will be times when you forget to wrap your string in new ObjectId()

  • it requires server side object creation to maintain _id uniqueness

  • which makes generating them client-side by minimongo problematic

String Pros

  • developers can create domain specific _id topologies

String Cons

  • developer has to ensure uniqueness of _ids

  • findAndModify() and getNextSequence() queries may be invalidated

Upvotes: 0

WiredPrairie
WiredPrairie

Reputation: 59763

The only difference is that one is actually an ObjectId and the other is a string representation of what looks to be an ObjectId.

DBRef as an ObjectId:

db.persons.insert({name: 'cavin', 
     sister: new DBRef('persons', 
         new ObjectId('5165419064fada69cef33ea2'))}) // use an ObjectId

DBRef as a String:

db.persons.insert({name: 'bob', 
     sister: new DBRef('persons', 
        '5165419064fada69cef33ea2')}) // use a string

In the example you included, the ObjectId format could result in more efficient storage as it's a 12-byte value instead of the 24 bytes that the string representation would require. If you wanted to use DBRefs, I'd use an ObjectId if the referenced collection is using ObjectIds for the _id.

You aren't required to use an ObjectId in a DBRef. It can be any value that represents the key (_id) of the related collection/DB.

As the documentation suggests, unless you have a compelling reason for using a DBRef, use manual references instead.

Upvotes: 6

Related Questions