如何使用$ lookup填充ID的结果数组

时间:2019-03-22 00:46:22

标签: javascript node.js database mongodb mongoose

我正在尝试查找用户上传的给定项目的所有评论。我想在用户个人资料页面中显示他所有已上传产品的列表,并且每个项目都希望显示一些评论。为了实现这一点,我试图将所有评论按项目分组。首先,我尝试通过汇总进行操作,以便将所有评论按项目分组,然后,我想在汇总结果中填充 item ,以获取标题和评分等字段。另外,由于要获取撰写评论的用户名,我尝试填充 reviews.user 。这不是一个困难的查询,但是$ lookup的语法阻止了我实现这一点。感谢您提前提供的所有帮助。

我有这些架构

const reviewSchema = new Schema(
    {
            text: { type: String, max: [400, 'Too long, max is 400 characters']},
            createdAt: { type: Date, default: Date.now },
            rating : { type: Number },
            user: {type: Schema.Types.ObjectId, ref:'User'},//User who wrote review
            item: {type: Schema.Types.ObjectId, ref:'Item', default: null},// Review belongs to this item
    });

const userSchema = new Schema(
    {
            username: { type: String, max: [20, 'Too long, max is 20 characters']},
            createdAt: { type: Date, default: Date.now },
            rating : { type: Number },
            email: { type: Number },
            items: [{type: Schema.Types.ObjectId, ref:'Item'}]
            // Some more info
    });


const itemSchema = new Schema(
    {
            title: { type: String, max: [40, 'Too long, max is 40 characters']},
            rating: { type: number },
            user : {type: Schema.Types.ObjectId, ref:'User'},
            // Some more info
    });

到此为止,我注意到必须在聚合管道中填充一个数组。

  const resolvedItemReviews = await Promise.all(user.items.map(async (rental) =>{
      return new Promise((resolve, reject) => 
      {
      Review.aggregate([
                   { "$match": {$and: [
                                       {item: item._id}, 
                                       {_id: { $nin: queryObj.seenIDs }}
                                   ]}},
                       { '$limit' : 5},
                       { "$sort": { "createdAt": 1 } },
                       { "$group": { 
                               "_id": "$item",
                               "item": {'$first': '$item'},
                               "reviews": { $push: {"text": "$text","user": "$user","rating": "$rating" ,"createdAt": "$createdAt"} },
                                }
                            },
                            // unwind source
                            {"$unwind": "$reviews"},
                            { "$lookup": {
                                "from": "User",
                                "localField": "reviews.user",
                                "foreignField": "_id",
                                "as": "reviewUser"
                            }},
                            { "$unwind": "$reviewUser" },
                        ],
                        async function(err,results) {
                            if (err) reject(err);
                            console.log(results)

                            await Item.populate(results, { "path": "rental", select: 'title image rating'})
                            resolve( results )
                    })
                });
            })

我目前正在得到这个结果:

[ { _id: 5c94061acd2e4e50a4e921aa,
item: 5c94061acd2e4e50a4e921aa,
reviews:
 { text:
    'Lorem ipsum dolor sit amet',
   user: 5c1c774ccc273b1584925b9d,
   rating: 5,
   createdAt: 2019-03-21T21:49:03.510Z } },
  { _id: 5c94061acd2e4e50a4e921aa,
item: 5c94061acd2e4e50a4e921aa,
reviews:
 { text:
    'Lorem ipsum dolor sit amet,',
   user: 5c5a3cb031c62708c4143f82,
   rating: 5,
   createdAt: 2019-03-21T22:10:10.013Z } } ]

这将是预期的结果

[ { _id: 5c940eb1c1d32b4fdc5c7ef4,
item: {
  _id: 5c940eb1c1d32b4fdc5c7ef4
  title: "Item title text",
  rating: 4.3,
  image: "https://www.imageurl.com",
},
reviews:
 { text: 'Some review text',
   user: {
     username: "TestUser",
     rating: 4.6,
     image: "https://www.user-imageurl.com",
   },
   rating: 4,
   createdAt: 2019-03-21T22:23:59.105Z } 
  },

 { text: 'Some review text from another user',
   user: {
     username: "TestUser 2",
     rating: 3.1,
     image: "https://www.user2-imageurl.com",
   },
   rating: 3,
   createdAt: 2019-02-21T22:23:59.105Z } 
   }
 ]

0 个答案:

没有答案