如何通过IsLiked字段为false的位置填充用户id

ymzxtsji  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(351)

我需要从likedby填充用户,其中IsDisked为false,请查找文档架构。

我正在使用下面的查询,但无法填充用户。使用下面的查询,

  1. let likedBy = await StoryComment.aggregate([{"$match":{"_id": exportLib.ObjectId(currentPost)}}, {"$match":{"comments.likedBy.isDisliked": false}}]);

得到如下所示的输出,

  1. [
  2. {
  3. _id: 60fd5b5336a2780754021ce0,
  4. comments: { reply: [], likedBy: [Array], report: [] },
  5. isDeleted: false,
  6. isArchived: false,
  7. storyId: 60fa45be78e0cc052760922f,
  8. comment: 'New to this place bro',
  9. userId: 60e7ce8e0812ba086dc16074,
  10. createdAt: 2021-07-25T12:38:43.427Z,
  11. updatedAt: 2021-07-25T13:46:28.728Z,
  12. __v: 0
  13. }
  14. ]

预期产出:

  1. [
  2. {
  3. isDisliked: false,
  4. _id: 60fd5d08ddf94d07d0da9d57,
  5. likedBy: 60e7ce8e0812ba086dc16074
  6. },
  7. {
  8. isDisliked: false,
  9. _id: 60fd6b34fa5bdf06a743fc3c,
  10. likedBy: 60df550da01794081fc83762
  11. }
  12. ]

需要likedby位置的用户详细信息(就像我们简单地使用填充一样),这只是likedby数组的非结构化版本。
请在此处找到架构:

  1. const storyComments = new mongoose.Schema(
  2. {
  3. storyId: { type: schema.Types.ObjectId, ref: "stories" },
  4. comment: { type: String },
  5. userId: { type: schema.Types.ObjectId, ref: "Users" },
  6. comments: {
  7. reply: [{
  8. comment: { type: String },
  9. userId: { type: schema.Types.ObjectId, ref: "Users" },
  10. }],
  11. likedBy : [{
  12. likedBy: { type: schema.Types.ObjectId, ref: "Users" },
  13. isDisliked: { type: Boolean, default: false}
  14. }],
  15. report : [{
  16. reportedBy: { type: schema.Types.ObjectId, ref: "Users" },
  17. reason: { type: String },
  18. isReported: { type: Boolean, default: false },
  19. isAdminApproved: { type: Boolean, default: false },
  20. isCreatorApproved: { type: Boolean, default: false },
  21. creatorEmail: { type: String },
  22. tokenToApprove: { type: String, default: ""}
  23. }]
  24. },
  25. isDeleted: { type: Boolean, default: false },
  26. isArchived: { type: Boolean, default: false }
  27. },
  28. {
  29. timestamps: true,
  30. }
  31. );

请帮我把这个修好。谢谢

92vpleto

92vpleto1#

您可以尝试这样来填充用户

  1. const story = StoryComment.find({
  2. "_id": exportLib.ObjectId(currentPost),
  3. "comments.likedBy": {
  4. "$elemMatch": {
  5. "isDisliked": true
  6. }
  7. }
  8. },
  9. {
  10. "comments.likedBy.$": 1
  11. })
  12. .populate('Users')
  13. .exec(function (err, person) {
  14. if (err) return handleError(err);
  15. console.log(person);
  16. });

以下是官方文件的链接:参考Children

如果你想通过 aggregate 你可以试试这段代码

  1. db.story.aggregate([
  2. {
  3. "$match": {
  4. "_id": ObjectId("5a934e000102030405000005")
  5. }
  6. },
  7. {
  8. "$unwind": "$comments.likedBy"
  9. },
  10. {
  11. "$match": {
  12. "comments.likedBy.isDisliked": false
  13. }
  14. },
  15. {
  16. "$project": {
  17. likedBy: "$comments.likedBy.likedBy"
  18. }
  19. },
  20. {
  21. "$lookup": {
  22. "from": "users",
  23. "localField": "likedBy",
  24. "foreignField": "_id",
  25. "as": "user"
  26. }
  27. },
  28. {
  29. "$unwind": "$user"
  30. },
  31. {
  32. $replaceRoot: {
  33. newRoot: "$user"
  34. }
  35. }
  36. ])

这里是到Playground的链接,用于测试您的用例:mongoPlayground

展开查看全部

相关问题