'i need to pick the details of post from postCollection how to lookup in mongoose?

[
  {
    _id: new ObjectId("6236ba6b465ff537abfaa64c"),
    firstName: 'user',
    lastName: 'uer',
    password: '$2a$10$2SX2R1lFEErvcJnO1oGafOK/BRnQ.PG7oLHX0LJ/uuEhrk81NcbnO',
    email: '[email protected]',
    phone: 5465465645,
    premiumUser: false,
    status: true,
    authorStatus: false,
    indroduction: 'dfghjk',
    city: 'asdfadf',
    state: 'asdfasdf',
    address: 'sdfghjksdaf',
    wishlistItems: [
      new ObjectId("6231db05a8b12caba4feb39a"),
      new ObjectId("6233118f2f06967703e70a4b"),
      new ObjectId("6231daa03eedc2006c23b98d"),
      new ObjectId("6231d3f1b7b4d0299b8b5e2f"),
      new ObjectId("6230dbf77f64b9b25f3939c0"),
      new ObjectId("6230d44f6250bcb698a4a5fb")
    ],
    createdAt: 2022-03-20T05:23:55.086Z,
    updatedAt: 2022-03-20T05:25:20.516Z,
    __v: 0
  }
]

the above the is the userCollections.

postCollection below is my postCollection

{
    _id: ObjectId('62330fb82f06967703e70a48'),
    postTitle: 'GET A PEEK INTO THE FORGOTTEN SIDE OF ASAKUSA,TOKYO',
    subTitle: 'Are you a first time visitor to Japan and looking ..',
    postIndroduction: 'Are you a first time visitor to Japan and ..',
    postContent: 'Pearl Harbor Attack had awakened a sleeping gia.',
    place: 'JAPAN',
    date: ISODate('2016-05-12T00:00:00.000+00:00'),
    authorId: ObjectId('622ad301141ddb7050a78634'),
    likeCount: 0,
    image1:'https://res.cloudinary.com/ashproduct/image/upload/v1647513503/pzhaddu...',
image2:'https://res.cloudinary.com/ashproduct/image/upload/v1647513506/guooaum...',
image3:'https://res.cloudinary.com/ashproduct/image/upload/v1647513514/rbyh2p4...',
image4:'https://res.cloudinary.com/ashproduct/image/upload/v1647513519/vwwqerz...',
image5:'https://res.cloudinary.com/ashproduct/image/upload/v1647513527/xqyukzj...',
    ....
};

in my project i need to make wishlist for users. so i make an array list for storing the id of the posts that liked by the user.this wishlist array present in useCollection. its done....But now i need to get that post details to show in another page wishlistItem page.

here i need to get all details of single post from another collection called postCollection on the basis of id present in the collections array userCollection.wishListItems.i am using "mongoose": "^6.2.4"

what should i do to get the result? what is syntax of aggregation for apply this? anyOne please help....



Solution 1:[1]

Pretty sure you are looking for "$lookup". Something like:

db.userCollections.aggregate([
  {
    "$match": {
      "_id": ObjectId("6236ba6b465ff537abfaa64c")
    }
  },
  {
    "$lookup": {
      "from": "postCollections",
      "localField": "wishlistItems",
      "foreignField": "_id",
      "as": "wishPosts"
    }
  }
])

Try it on mongoplayground.net.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rickhg12hs