I have got two collections in MongoDB:
User collection:
{
id : 1,
name : "John",
username : "Ricky1",
}
Post collection:
{
id : 1,
title : "mongodb collections",
userİd : 1,
}
How do I merge these two collections?
I have got two collections in MongoDB:
User collection:
{
id : 1,
name : "John",
username : "Ricky1",
}
Post collection:
{
id : 1,
title : "mongodb collections",
userİd : 1,
}
How do I merge these two collections?
I don't think it can be done. You will need to do 2 queries:
One to find the user, and another to get all posts filtering by that userId.
for all posts by username "Ricky1":
db.post.find({userId:db.user.findOne({"username" : "Ricky1"}).id});
Maybe helps a little. The mongo way to do this, though, I gather, would be to have collections of posts nested inside each user in your users collection.