Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, very few drivers for MongoDb include special support for a <code>DBRef</code>. There are two reasons: </p> <ol> <li>MongoDb doesn't have any special commands to make retrieval of referenced documents possible. So, drivers that do add support are artificially populating the resulting objects. </li> <li>The more, "bare metal" the API, the less it makes sense. In fact, as. MongoDb collections are schema-less, if the NodeJs driver brought back the primary document with all references realized, if the code then saved the document without breaking the references, it would result in an embedded subdocument. Of course, that would be a mess.</li> </ol> <p>Unless your field values vary, I wouldn't bother with a <code>DBRef</code> type and would instead just store the <code>ObjectId</code> directly. As you can see, a <code>DBRef</code> really offers no benefit except to require lots of duplicate disk space for each reference, as a richer object must stored along with its type information. Either way, you should consider the potentially unnecessary overhead of storing a string containing the referenced collection's documents. </p> <p>Many developers and MongoDb, Inc. have added an object document mapping layer on top of the existing base drivers. One popular option for MongoDb and Nodejs is Mongoose. As the MongoDb server has no real awareness of referenced documents, the responsibility of the references moves to the client. As it's more common to consistently reference a particular collection from a given document, Mongoose makes it possible to define the reference as a Schema. Mongoose is not schema-less. </p> <p>If you accept having and using a Schema is useful, then Mongoose is definitely worth looking at. It can efficiently fetch a batch of related documents (from a single collection) from a set of documents. It always is using the native driver, but it generally does operations extremely efficiently and takes some of the drudgery out of more complex application architectures. </p> <p>I would strongly suggest you have a look at the <code>populate</code> method (<a href="http://mongoosejs.com/docs/populate.html" rel="nofollow">here</a>) to see what it's capable of doing. </p> <pre><code>Demo /* Demo would be a Mongoose Model that you've defined */ .findById(theObjectId) .populate('detail') .exec(function (err, doc) { if (err) return handleError(err); // do something with the single doc that was returned }) </code></pre> <p>If instead of <code>findById</code>, which always returns a single document, <code>find</code> were used, with <code>populate</code>, all returned documents' <code>details</code> property will be populated automatically. It's smart too that it would request the same referenced documents multiple times. </p> <p>If you don't use Mongoose, I'd suggest you consider a caching layer to avoid doing client side reference joins when possible and use the <code>$in</code> query operator to batch as much as possible. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload