gpt4 book ai didi

javascript - mongoDB 中的分组依据

转载 作者:行者123 更新时间:2023-11-28 15:23:02 24 4
gpt4 key购买 nike

我会尽力解释自己,我有这个集合,我想以某种方式对我的集合执行“分组依据”......拥有一个如下所示的集合:

[
{
"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..} ]
}
]

我已经准备好了像这样的groupBy

db.myCollection.aggregate({
"$group": {
"_id": "$city",
"resources": {
"$push": "$person"
}
}
})

但我找不到将关键“population”添加到每个结果的方法。(人口和城市的值不会改变,我的意思是如果城市的一个值为“city1”,则人口的值将始终“太多”)

最佳答案

使用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()

关于javascript - mongoDB 中的分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30463897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com