我需要从 postCollection 中选择帖子的详细信息如何在猫鼬中查找?

发布于 2025-01-15 11:59:37 字数 2372 浏览 2 评论 0原文

[
  {
    _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
  }
]

上面是userCollections

下面的 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...',
    ....
};

我项目中的 postCollection,我需要为用户制作愿望清单。所以我创建了一个数组列表来存储用户喜欢的帖子的id。这个愿望清单数组存在于useCollection中。完成了......但现在我需要获取该帖子的详细信息以显示在另一个页面 wishlistItem 页面中。

在这里,我需要根据集合数组 userCollection.wishListItems 中存在的 id 从另一个名为 postCollection 的集合中获取单个帖子的所有详细信息。我正在使用 "mongoose": "^6.2.4"

我应该怎么做才能得到结果?应用此聚合的语法是什么?任何人请帮忙....

[
  {
    _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....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光飞翔i 2025-01-22 11:59:37

很确定您正在寻找“$lookup”。例如:

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

mongoplayground.net 上尝试一下。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文