Javascript mongoDB中的分组
我会尽我所能解释我自己,我有这个收藏,我想对我的收藏进行一次“分组”。。拥有一个如下所示的集合:Javascript mongoDB中的分组,javascript,mongodb,mongoose,Javascript,Mongodb,Mongoose,我会尽我所能解释我自己,我有这个收藏,我想对我的收藏进行一次“分组”。。拥有一个如下所示的集合: [ { "city": "City1", "population": "too many", "person": {//..some object..} }, { "city": "City1", "population": "too many", "person": {//..other object..} }, {
[
{
"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"
}
}
})
我已经准备好做这样的群比了
[
{
"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"
}
}
})
但我找不到一种方法将关键的“总体”添加到每个结果中。
(人口和城市的值不变,我的意思是如果城市的一个值是“city1”,人口的值总是“太多”)使用mongo,查找城市是否存在,并按如下方式查询:
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()
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()
新编辑
根据问题要求,如果您的文件像这样
{ "_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", //group by city
"data": {
"$push": {
"name": "$person.name",
"age": "$person.age",
"address": "$person.address"
}
}
}
}, {
"$project": {
"_id": 0,
"city": "$_id",
"person": "$data"
}
}).pretty()
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()
使用mongo和查找是否存在城市,并按如下方式进行查询:
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()
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()
新编辑
根据问题要求,如果您的文件像这样
{ "_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", //group by city
"data": {
"$push": {
"name": "$person.name",
"age": "$person.age",
"address": "$person.address"
}
}
}
}, {
"$project": {
"_id": 0,
"city": "$_id",
"person": "$data"
}
}).pretty()
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()
这与我想做的很接近,但是如果集合像这样:
code
[{“城市”:“城市1”,“人”:{/..some object….},{“城市”:“城市1”,“人”:{/..other object….},{“城市”:“城市1”,“人”:{/..other object….},{“城市2”{/,“person”:{/..one object..}}]code
@flaalf我不明白你在问什么?你的收藏有不同的数据吗?这接近于我想做的,但是如果像这样的收藏:code
[{“城市”:“City1”,“person”:{/..some object..},{“城市”,会有多大的不同“:”City1“,”person“:{/..other object..},{”City1“,”person“,{/..other object..},{”city“:”City2“,”person“:{/..one object..}}]code
@flaalf我不明白你在问什么?您的收藏是否有不同的数据?