Question

If I choose to use Document References with a structure of Materialized Paths instead of the simple Embedded Documents how can I display the same results?

For example if I had Embedded docs I simply :

db.col.find({'user' : 'foo'}) 

and return:

{'user' : 'foo',
 'posts' : [ {}, 
             {},
             {} 
  ]
}

Which command should I use to display posts as an embedded array of that user? Or this can only happen client-side?

Was it helpful?

Solution

If it's document references,

users collection will contain:

{
  _id : "foo",
  // users details
}

and posts collection:

{
   _id: "postid",
   author: "foo"
   // other fields
}

In this case,

1) First make query to get the user id from users collection.

2) Then send the user id to the posts collection to get all posts

var user = db.users.find({_id : "foo"}); 

// this is used to get user details or validate user and only after validation if you need to fetch the posts

var posts = db.posts.find({author:  user._id });

As the documents are referenced, there will be a roundtrip to the server which is obvious.

I am not sure how you have used materialized path for this scenario, let me know the data structure of it and i would be able to mention the query based on that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top