1

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?

Community
  • 1
  • 1
  • 4
    MongoDB does not do joins. One way of working around that fact would be to denormalize the documents/collections. – Joachim Isaksson Feb 13 '14 at 22:21
  • 2
    I'm surprised these questions still get asked, you would think a person would look into joins in MongoDB before making something in it :\ – Sammaye Feb 13 '14 at 22:27
  • Thanks for answer. How can I merge collections in iformation ? – user3308059 Feb 13 '14 at 22:27
  • Sad state that MongoDB still cannot serve this obvious need. There must be one question per day about this on Stack Overflow. Isn't that customer demand enough? – usr Feb 13 '14 at 22:29
  • 3
    The title of the question alone would have been answered with a simple search. The sad thing about people continuing to ask is that they do not understand the product they are using. – Neil Lunn Feb 14 '14 at 00:34
  • Can't agree more with people above. No offence, man, you should get to know the product before using it. MongoDB doesn't work the way RDBMS works. – yaoxing Feb 14 '14 at 03:50
  • @usr That's because MongoDB newbies often don't understand the difference between a document-oriented and a relational database. When they try out MongoDB for the first time they still think in relational terms. So they make up fully normalized data models which depend on JOINs and then are surprised that MongoDB doesn't support that. – Philipp Feb 14 '14 at 11:52
  • possible duplicate of [MongoDB and "joins"](http://stackoverflow.com/questions/4067197/mongodb-and-joins) – Philipp Feb 14 '14 at 11:54
  • 1
    @Philipp the thing is, almost no real-world data model can live without relations and joins. This makes the product very narrowly applicable. It leads to developers doing the join in their own code because they simply need the join. – usr Feb 14 '14 at 11:54
  • @usr MongoDB does place joins on the client side, that is the point, you are supposed to do it in your own code – Sammaye Feb 15 '14 at 00:39

3 Answers3

2

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.

Joel
  • 4,732
  • 9
  • 39
  • 54
0

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.

Peter Perháč
  • 20,434
  • 21
  • 120
  • 152
0

Here Example Inner Join

db.users.aggregate({ $lookup: {
from : "courses", 
localField: "courseId", 
foreignField: "_id",
 as : "usersInCourse"}}
,{ $match : { "usersInCourse" : { $ne : []}}})

Above MongoDB Query Output

{
  _id: ObjectId("643404232a8430b9d86ec42b"),
  name: 'Ishan',
  isVerified: true,
  courseId: [
    ObjectId("643402ebae34b1cbab3bb377")
  ],
  usersInCourse: [
    {
      _id: ObjectId("643402ebae34b1cbab3bb377"),
      CourseName: 'MongoDB Basics',
      price: 50
    }
  ]
},{
  _id: ObjectId("643404232a8430b9d86ec42c"),
  name: 'Bhavesh',
  isVerified: true,
  courseId: [
    ObjectId("643402ebae34b1cbab3bb377"),
    ObjectId("643402ebae34b1cbab3bb378")
  ],
  usersInCourse: [
    {
      _id: ObjectId("643402ebae34b1cbab3bb377"),
      CourseName: 'MongoDB Basics',
      price: 50
    },
    {
      _id: ObjectId("643402ebae34b1cbab3bb378"),
      CourseName: 'NodeJS',
      price: 50
    }
  ]
}

In users collection data

{
  _id: ObjectId("643404232a8430b9d86ec42b"),
  name: 'Ishan',
  isVerified: true,
  courseId: [
    ObjectId("643402ebae34b1cbab3bb377")
  ]
},
{
  _id: ObjectId("643404232a8430b9d86ec42c"),
  name: 'Bhavesh',
  isVerified: true,
  courseId: [
    ObjectId("643402ebae34b1cbab3bb377"),
    ObjectId("643402ebae34b1cbab3bb378")
  ]
},
{
  _id: ObjectId("64340835ae34b1cbab3bb38c"),
  name: 'Amol',
  isVerified: true,
  courseId: []
}

In courses collection data

{
  _id: ObjectId("643402ebae34b1cbab3bb378"),
  CourseName: 'NodeJS',
  price: 50
},
{
  _id: ObjectId("643402ebae34b1cbab3bb377"),
  CourseName: 'MongoDB Basics',
  price: 50
}

Amol
  • 55
  • 4