James P. Wright
James P. Wright

Reputation: 9131

Mongoose joining data

If I have an object in my MongoDB that will need to be used EVERYWHERE in my system, so it is in its own collection. However, I con't quite figure out how to get the data to show up automatically on the other objects it is joined to.
Here is an example:

Schema1 = { name: String }  
Schema2 = { something: String, other_thing: [{schema1_id: String}] }

Now what I want is to be able to say var name = mySchema2.name; and get the name of the linked Schema1 object.
I am using Mongoose, Express and Node.js and I have tried using a Mongoose 'virtual' for this, but when I say res.send(myobject); I don't see the virtual property anywhere on the object.

What is the best way to do this?

Upvotes: 4

Views: 7839

Answers (3)

Kfir Erez
Kfir Erez

Reputation: 3470

I know it is far after you post the question but it might help others.
If you use this reference all over you may want to consider using embedded document. The benefits of embedded document is that you get them when you query the parent document thus it save you additional query and the drawbacks is that the parent document may become large (or even very large) thus you should use them but use them carefully.
Here is an example of simple embedded document. Instead of referencing 'comments' in the post document, which require additional query, we will embed it (code is a bit pseudo):

var postSchema = new Schema({
  author : {type : String}, 
  title : {type : String, required : true},
  content : {type : String, required : true},
  comment : {
    owner : {type : String},
    subject : {type: String, required : true},
    content : {type String, required : true}
  }
});

MongoDB allows you a simple and convenience way to query comments' fields by the dot character. For example if we like to query only comments which their subject starts with 'car' we do as follow:

myPostModel.find({ 'comment.subject' : /car*/ }).exec(function(err, result){
    Do some stuff with the result...
});

Note that for simplicity of the example the comment field in the post is not an array (one comment per post is allowed in this example). However even if it will be an array, mongo refer to array's elements very elegantly in the same way.

Upvotes: 3

evilcelery
evilcelery

Reputation: 16139

There are a couple of plugins to help with DBRefs in Mongoose.

mongoose-dbref uses the DBRef standards and is probably a good place to start.

mongoose-plugins is one I wrote a while ago but it works in a slightly different way.

Upvotes: 1

Andrew Orsich
Andrew Orsich

Reputation: 53685

In mongodb no such word as JOIN, because of joins killing scalability. But most drivers support DBRefs, they are just making additional request to load referenced data. So you can just make additional request yourself to load object that you using everywhere.

If you using some object everywhere in your app it sounds like object that need to be in cache. But mongodb work as some kind o cache if enough memory to load object into memory. So, to keep it simple just make additional request to load object.

Upvotes: -1

Related Questions