2016-06-01 34 views
0

我想根据特定值对文档进行分组。我还需要选择的键。Mongo DB组记录并返回所选字段

这里是我的文档:

{ 
    title: "Add 1", 
    userId: 1, 
    description: "this is add 1", 
    location: { 
     city: "Newyork", 
     address: "Street 1, Flat # 40" 
     }, 
    createdAt: "2015-10-20", 

    title: "Add 2", 
    userId: 1, 
    description: "this is add 2", 
    location: { 
     city: "Paris", 
     address: "Street 2, Flat # 80" 
     }, 
    createdAt: "2015-10-22", 

    title: "Add 3", 
    userId: 1, 
    description: "this is add 3", 
    location: { 
     city: "Newyork", 
     address: "Street 5, Flat # 58" 
     }, 
    createdAt: "2015-10-23" 
} 

我想组文件由location.city。我也只需要标题和位置。

以上情况应该查询什么?

我曾尝试此查询:

Adds.aggregate([ 
        { $match: { userId: params.userId } }, 
        { $group: { _id: "$location.city" } },    
       ]); 

上述查询只返回location.city,我想要的位置&冠军的完整的对象。

+1

你可以把它用.aggregate如果这个数据是在一个数组分解做。不是一个单一的文件。 – Tiramisu

+0

@Tiramisu我已经尝试使用聚合,但响应结果不好。我使用location.city对记录进行分组,因此它只返回location.city。请检查更新的问题 – StormTrooper

回答

0

假设你的数据实际上是这样的:

[ 
    { 
    title: "Add 1", 
    description: "this is add 1", 
    location: { 
     city: "Newyork", 
     address: "Street 1, Flat # 40" 
     }, 
    createdAt: "2015-10-20", 
    }, 
    { 
    title: "Add 2", 
    description: "this is add 2", 
    location: { 
     city: "Paris", 
     address: "Street 2, Flat # 80" 
     }, 
    createdAt: "2015-10-22", 
    }, 
    { 
    title: "Add 3", 
    description: "this is add 3", 
    location: { 
     city: "Newyork", 
     address: "Street 5, Flat # 58" 
     }, 
    createdAt: "2015-10-23" 
    } 
] 

你可以使用一个汇总查询,例如:

db.adds.aggregate([ 
    { $group : 
     { 
      _id : "$location.city", 
      data : { $push : { "title" : "$title", "location" : "$location" } } 
     } 
    } 
]); 
+0

您发送给我的查询将返回2条记录,现在有两个记录与城市纽约克,它将被分组为一条记录。所以当分组时,它会带来哪些记录,其中“地址”为“街道5,公寓#58”还是“地址”为“街道1,公寓#40”? – StormTrooper

+0

如果您指出结果应该是什么样子,那么我们可以相应地调整查询。 – CodeMan

0

userID是缺少文档,如果您的收藏看起来像这样你聚合函数是正确的。

{ 
    "title": "Add 1", 
    "userId": 1, 
    "description": "this is add 1", 
    "location": { 
     "city": "Newyork", 
     "address": "Street 1, Flat # 40" 
     }, 
    "createdAt": "2015-10-20" 
}, 
{ 
    "title": "Add 2", 
    "userId": 2 
    "description": "this is add 2", 
    "location": { 
     "city": "Paris", 
     "address": "Street 2, Flat # 80" 
     }, 
    "createdAt": "2015-10-22" 
}, 
{ 
    "title": "Add 3", 
    "userId": 1, 
    "description": "this is add 3", 
    "location": { 
     "city": "Newyork", 
     "address": "Street 5, Flat # 58" 
     }, 
    "createdAt": "2015-10-23" 
} 

测试使用MongoDB的控制台

db.Adds.aggregate([ 
      { $match: { userId: 1 } }, 
      { $group: { _id: "$location.city" } } 
]); 

添加标题您的结果

db.Adds.aggregate([ 
    { $match: { userId: 1 } }, 
    { 
     $group: { 
      _id: "$location.city", 
      title: {$push: "$title"} 
     } 
    } 
]); 
+0

是的我的错误我跳过了文档中的userId。我有一个问题。你发给我的查询将返回2条记录,现在有两个记录与城市纽约克,它将被分组为一条记录。所以当分组时,它会带来哪些记录,其中“地址”为“街道5,公寓#58”还是“地址”为“街道1,公寓#40”? – StormTrooper

+0

如果要标识每条记录,聚合函数将不起作用。所以小组返回了一个像这样的新文档,你不能说这个用户是什么。 {“_id”:“Newyork”,“title”:[“Add 1”,“Add 3”]} –