2016-06-01 54 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」]} –