No, very few drivers for MongoDb include special support for DBRef . There are two reasons:
- MongoDb does not have special commands to make the search for referenced documents possible. Thus, drivers that add support artificially populate the resulting objects.
- The larger the bare metal API, the less it makes sense. Actually, like. MongoDb collections have no schema, if the NodeJs driver returned the primary document with all the links implemented, if the code then saved the document without breaking the links, this will lead to an embedded document. Of course it will be a mess.
If the field values change, I would not worry about the DBRef type and instead just save the ObjectId directly. As you can see, DBRef really does not offer any advantages, except that each link requires a lot of duplicated disk space, since a richer object must be stored along with its type information. In any case, you should consider the potentially unnecessary overhead of storing a string containing collection reference documents.
Many developers and MongoDb, Inc. added an object document display layer on top of existing base drivers. One of the popular options for MongoDb and Nodejs is Mongoose. Since the MongoDb server does not have a real understanding of the referenced documents, the responsibility for the links is transferred to the client. Since most often a particular collection from this document is sequentially referenced, Mongoose allows you to define a link as a schema. Mongoose is not without a pattern.
If you agree that using and using a schema is useful, then Mongoose is definitely worth a look. He can effectively receive a package of related documents (from one collection) from a set of documents. It always uses its own driver, but it usually works extremely efficiently and takes some of the complexity out of more complex application architectures.
I would strongly suggest you take a look at the populate method ( here ) to see what it is capable of doing.
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 })
If instead of findById , which always returns a single document, find was used, when populate property of all returned documents details will be filled automatically. It is also smart that he will request the same reference documents several times.
If you are not using Mongoose, I would advise you to think about caching in order to avoid possible use of client-side links and use the $in query operator to batch load as much as possible.
Wiredprairie
source share