How to do find in node and mongodb?

前端 未结 3 729
无人共我
无人共我 2021-01-22 22:25

This is my \'usergroups\' data

{
    \"_id\": {
        \"$oid\": \"58f7537ec422895572e988a1\"
    },
    \"name\": \"aaa\",
    \"groupname\": \"group north,gro         


        
3条回答
  •  遥遥无期
    2021-01-22 22:56

    You can use the below aggregation for 3.4 version.

    The query will $match documents in the usergroups collection with the mobilenumber followed by $split to split the groupnames and $lookup the groupname in the groups collection for messages. Final step is to $unwind the mobile_group array to $project the messages for each groupname.

    db.usergroups.aggregate([
        { $match: { mobilenumber:"0509867865" } },
        { $project: { groupname:{ $split: [ "$groupname", ',' ] }  } },
        { $lookup:
            {
              from: "groups",
              localField: "groupname",
              foreignField: "groupname",
              as: "mobile_group"
            }
       },
       {$unwind:"$mobile_group"},
       { $project : { groupname:"$mobile_group.groupname", "messages" : "$mobile_group.message" } }
    ])
    

提交回复
热议问题