Mongo db Query用于过滤文档中嵌套的对象数组

时间:2018-02-28 13:53:46

标签: node.js mongodb mongoose mongodb-query aggregation-framework

我有以下文件

SELECT 
    this.amount as current_amount, 
    this.weekno, 
    history.amount AS past_amount
  • 第一次查询

这里我想从friends数组中获取userId,其状态等于" ACCEPT"。 即

{
    "userid": "5a88389c9108bf1c48a1a6a7",
    "email": "abc@gmail.com",
    "lastName": "abc",
    "firstName": "xyz",
    "__v": 0,
    "friends": [{
        "userid": "5a88398b9108bf1c48a1a6a9",
        "ftype": "SR",
        "status": "ACCEPT",
        "_id": ObjectId("5a9585b401ef0033cc8850c7")
    },
    {
        "userid": "5a88398b9108bf1c48a1a6a91111",
        "ftype": "SR",
        "status": "ACCEPT",
        "_id": ObjectId("5a9585b401ef0033cc8850c71111")
    },
    {
        "userid": "5a8ae0a20df6c13dd81256e0",
        "ftype": "SR",
        "status": "pending",
        "_id": ObjectId("5a9641fbbc9ef809b0f7cb4e")
    }]
},
{
    "userid": "5a88398b9108bf1c48a1a6a9",
    "friends": [{ }],
    "lastName": "123",
    "firstName": "xyz",
    .......
},
{
    "userid": "5a88398b9108bf1c48a1a6a91111",
    "friends": [{ }],
    "lastName": "456",
    "firstName": "xyz",
    ...
}   
  • 第二次查询

之后,我必须对同一个集合进行另一个查询,以获取第一个查询中返回的每个用户标识的详细信息。  final Query将返回 [5a88398b9108bf1c48a1a6a9,5a88398b9108bf1c48a1a6a91111] 的详细信息  用户ID即

[5a88398b9108bf1c48a1a6a9,5a88398b9108bf1c48a1a6a91111]

到目前为止,我已经尝试了

[
        {
         userid" : "5a88398b9108bf1c48a1a6a9",
         "lastName" : "123",
         "firstName" : "xyz"
         },
       {
         "userid" : "5a88398b9108bf1c48a1a6a91111",
          "lastName" : "456",
           "firstName" : "xyz"
       }
   ]

2 个答案:

答案 0 :(得分:2)

使用聚合框架的 $map $filter 运算符来处理该任务。 $filter 会根据状态应该等于"ACCESS" $map 的指定条件过滤好友数组。过滤后的数组为所需的格式。

对于第二个查询,请附加 $lookup 管道步骤,该步骤在用户集合上执行自联接,以检索与上一个管道中的ID匹配的文档。

运行以下聚合操作将生成所需的数组:

User.aggregate([
    { "$match": { "friends.status": "ACCEPT" } },
    { "$project": {
            "users": {
                "$map": {
                    "input": {
                        "$filter": {
                            "input": "$friends",
                            "as": "el",
                            "cond": { "$eq": ["$$el.status", "ACCEPT"] }
                        }
                    },
                    "as": "item",
                    "in": "$$item.userid"
                }
            }
    } },
    { "$lookup": {  
        "from": "users",
        "as": "users",
        "localField": "users",
        "foreignField": "userid"
    } },
]).exec((err, results) => {
    if (err) throw err;
    console.log(results[0].users); 
});

答案 1 :(得分:1)

我没有测试它。只是为了一个想法,试一试让我知道。

 db.Users.aggregate(
       [
        {
           $unwind: "$friends"
        },
        {
          $match:{ "$friends.status": "ACCEPT"}
        },
        {
        $project:{ "FriendUserID":"$friends.userid"}
        },
        { 
          $lookup:{  
             from:"Users",
             as: "FriendsUsers",
             localField: "FriendUserID",
             foreignField: "userid"
          }
        },
        {
          $project: { FriendsUsers.lastName:1,FriendsUsers.firstName:1 }
        }
       ]
    )