我将尝试尽我所能解释自己,我有这个系列,我想通过"来执行一个"组。我的收藏品的方式是......有一个看起来像这样的集合:
[
{
"city": "City1",
"population": "too many",
"person": {//..some object..}
},
{
"city": "City1",
"population": "too many",
"person": {//..other object..}
},
{
"city": "City1",
"population": "too many",
"person": {//..another object..}
},
{
"city": "City2",
"population": "too low",
"person": {//..one object..}
}
]
输出就像这样
[
{
"city": "City1",
"population": "too many",
"person": [
{//..some object..},
{//..other object..},
{//..another object..}
]
},
{
"city": "City2",
"population": "too low",
"person":[ {//..one object..} ]
}
]
我已经做过像这样的小组
db.myCollection.aggregate({
"$group": {
"_id": "$city",
"resources": {
"$push": "$person"
}
}
})
但是我无法找到一种方法来为每个结果添加密钥" population"。 (人口和城市的价值不会发生变化,我的意思是,如果城市的一个价值是" city1",人口的价值总是很多"太多")
答案 0 :(得分:4)
使用mongo aggregation和$exists查找city
礼物与否,查询如下:
db.collectionName.aggregate({
"$match": {
"city": {
"$exists": true //check city presents or not
}
}
}, {
"$group": {
"_id": "$city", // group by city
"data": {
"$push": { // push all data into array
"name": "$name",
"age": "$age",
"address": "$address"
}
}
}
}, {
"$project": {
"city": "$_id",
"theResults": "$data", //project them
"_id": 0
}
}).pretty()
修改强>
根据您的第二个要求,请考虑您的文档结构:
{ "_id" : ObjectId("5564aaa1934833d8c1a1313f"), "city" : "City1", "person" : { "name" : "someName", "age" : "32", "address" : "Evergreen 124" } }
{ "_id" : ObjectId("5564aaa1934833d8c1a13140"), "city" : "City1", "person" : { "name" : "someName", "age" : "32", "address" : "Evergreen 125" } }
{ "_id" : ObjectId("5564aaa1934833d8c1a13141"), "city" : "City1", "person" : { "name" : "someName", "age" : "32", "address" : "Evergreen 126" } }
{ "_id" : ObjectId("5564aaa1934833d8c1a13142"), "city" : "City2", "person" : { "name" : "someName", "age" : "32", "address" : "Evergreen 129" } }
你应该使用这个聚合:
db.collectionName.aggregate({
"$group": {
"_id": "$city", //group by city
"data": {
"$push": {
"name": "$person.name",
"age": "$person.age",
"address": "$person.address"
}
}
}
}, {
"$project": {
"_id": 0,
"city": "$_id",
"person": "$data"
}
}).pretty()
新修改
根据this问题要求,如果你的文件是这样的
{ "_id" : ObjectId("5564aaa1934833d8c1a1313f"), "city" : "City1", "population" : "too many", "person" : { "name" : "someName", "age" : "32", "address" : "Evergreen 124" } }
{ "_id" : ObjectId("5564aaa1934833d8c1a13140"), "city" : "City1", "population" : "too many", "person" : { "name" : "someName", "age" : "32", "address" : "Evergreen 125" } }
{ "_id" : ObjectId("5564aaa1934833d8c1a13141"), "city" : "City1", "population" : "too many", "person" : { "name" : "someName", "age" : "32", "address" : "Evergreen 126" } }
{ "_id" : ObjectId("5564aaa1934833d8c1a13142"), "city" : "City2", "population" : "too low", "person" : { "name" : "someName", "age" : "32", "address" : "Evergreen 129" } }
然后你应该使用以下聚合:
db.collectionName.aggregate({
"$group": {
"_id": {
"city": "$city",
"population": "$population"
},
"data": {
"$push": {
"name": "$person.name",
"age": "$person.age",
"address": "$person.address"
}
}
}
}, {
"$project": {
"_id": 0,
"city": "$_id.city",
"population": "$_id.population",
"person": "$data"
}
}).pretty()