如何使用mongodb的$ project在文档中获取ID用户的值

时间:2019-06-26 16:26:53

标签: mongodb

我在mongo中有一个数据集合,我希望创建另一个集合,但只包含用户的数据,当显示连续性时,我只有ID。

   For example i have these data
{
id: ObjectId(1),
work: adadas,
user: ObjectId(8)
},
{
id: ObjectId(2),
work: adadas,
user: ObjectId(6)
},
{
id: ObjectId(3),
work: adadas,
user: ObjectId(2)
},
{
id: ObjectId(4),
work: adadas,
user: ObjectId(3)
}

 I immplement these code:


 db.getCollection("staff").aggregate([
 {
    $project: { 
        _id: 0,
        name: "$user", 
        phone: "$user", 
        dir: "$user" 
    }
  }
])

 I  want these result

{name: jon, dir: hada, phone: 2212313},{name: ale, dir: sdhada, phone: 2213454},{name: yas, dir: hfdgd, phone: 22126786},{name: jsan, dir: hadaddaa, phone: 2256456565},

0 个答案:

没有答案