我有这个文件:
{
"_id": "65b294740c5693e3deac0cea",
"name": "Islamic Republic Of Iran",
"population": 95000000,
"abb": "IRI",
"cities": [
{
"_id": "65b294740c5693e3deac0cf6",
"name": "Hamedan",
"population": 10,
"abb": "HM"
},
{
"_id": "65b294750c5693e3deac0cf7",
"name": "Tehran",
"population": 50,
"abb": "TH"
},
{
"_id": "65b294750c5693e3deac0cf8",
"name": "Kerman",
"population": 12,
"abb": "KM"
},
{
"_id": "65b294750c5693e3deac0cf9",
"name": "Esfahan",
"population": 25,
"abb": "ES"
},
{
"_id": "65b294750c5693e3deac0cfa",
"name": "Mashhad",
"population": 28,
"abb": "MS"
}
],
}
我想更新文档并通过以下数组在
city
字段上保存 5 个唯一的 cities
:
[
{
_id: "65b294750c5693e3deac0cfa",
name: "Mashhad",
abb: "MS",
population: 28
},
{
_id: "65b294750c5693e3deac0cfb",
name: "Tabriz",
abb: "TB",
population: 27
}
]
如果你注意的话,这个数组中
abb
和 population
字段的顺序与第一个数组不同。update pipeline
来更新 cities
字段:
[
{
"$set": {
"cities": {
"$filter": {
"input": "$cities",
"as": "citiesItem",
"cond": {
"$ne": [
"$$citiesItem._id",
"65b294750c5693e3deac0cf8"
]
}
}
}
}
},
{
"$set": {
"cities": {
"$setUnion": [
"$cities",
[
{
"_id": "65b294750c5693e3deac0cfa",
"name": "Mashhad",
"abb": "MS",
"population": 28
},
{
"_id": "65b294750c5693e3deac0cfb",
"name": "Tabriz",
"abb": "TB",
"population": 27
}
]
]
}
}
},
{
"$set": {
"cities": {
"$sortArray": {
"input": "$cities",
"sortBy": {
"_id": 1
}
}
}
}
},
{
"$set": {
"cities": {
"$slice": [
"$cities",
5
]
}
}
}
]
其实我很期待这个物体:
{
"_id": "65b294750c5693e3deac0cf8",
"name": "Kerman",
"population": 12,
"abb": "KM"
}
被擦除并且该对象:
{
_id: "65b294750c5693e3deac0cfb",
name: "Tabriz",
abb: "TB",
population: 27
}
添加到
cities
数组的末尾。Tabriz
的城市,而不是Mashhad
的城市,在cities
字段的末尾存储了两次,如下:
{
"_id": "65b294740c5693e3deac0cea",
"name": "Islamic Republic Of Iran",
"population": 95000000,
"abb": "IRI",
"cities": [
{
"_id": "65b294740c5693e3deac0cf6",
"name": "Hamedan",
"population": 10,
"abb": "HM"
},
{
"_id": "65b294750c5693e3deac0cf7",
"name": "Tehran",
"population": 50,
"abb": "TH"
},
{
"_id": "65b294750c5693e3deac0cf9",
"name": "Esfahan",
"population": 25,
"abb": "ES"
},
{
"_id": "65b294750c5693e3deac0cfa",
"name": "Mashhad",
"population": 28,
"abb": "MS"
},
{
"_id": "65b294750c5693e3deac0cfa",
"name": "Mashhad",
"abb": "MS",
"population": 28
}
],
}
我什至尝试使用下面的
pipeline
切换字段的顺序:
[
{
"$set": {
"cities": {
"$filter": {
"input": "$cities",
"as": "citiesItem",
"cond": {
"$ne": [
"$$citiesItem._id",
"65b294750c5693e3deac0cf8"
]
}
}
}
}
},
{
"$set": {
"cities": {
"$setUnion": [
{
"$project": {
"cities": {
"$map": {
"input": "$cities",
"as": "orderedItemcities",
"in": {
"_id": "$$orderedItemcities._id",
"name": "$$orderedItemcities.name",
"population": "$$orderedItemcities.population",
"abb": "$$orderedItemcities.abb"
}
}
}
}
},
[
{
"_id": "65b2976346bd44878b4ae02b",
"name": "Mashhad",
"abb": "MS",
"population": 28
},
{
"_id": "65b2976346bd44878b4ae02c",
"name": "Tabriz",
"abb": "TB",
"population": 27
}
]
]
}
}
},
{
"$set": {
"cities": {
"$sortArray": {
"input": "$cities",
"sortBy": {
"_id": 1
}
}
}
}
},
{
"$set": {
"cities": {
"$slice": [
"$cities",
5
]
}
}
}
]
但是我收到了这个错误:
Invalid $set : : caused by : : Unrecognized expression '$project'
我怎样才能为此创建一个合适的
pipeline
?keys
之一唯一地组合 MongoDB 中的两个数组?value
键的_id
来进行这个组合呢?
根据您的第二个查询,我修复了什么:
删除不需要的
$project
和 cities
。
abb
字段的位置应先于population
之前。
根据文档,“Mashhad”
_id
应该是“65b294750c5693e3deac0cfa”。
db.collection.update({},
[
{
"$set": {
"cities": {
"$filter": {
"input": "$cities",
"as": "citiesItem",
"cond": {
"$ne": [
"$$citiesItem._id",
"65b294750c5693e3deac0cf8"
]
}
}
}
}
},
{
"$set": {
"cities": {
"$setUnion": [
{
"$map": {
"input": "$cities",
"as": "orderedItemcities",
"in": {
"_id": "$$orderedItemcities._id",
"name": "$$orderedItemcities.name",
"abb": "$$orderedItemcities.abb",
"population": "$$orderedItemcities.population"
}
}
},
[
{
"_id": "65b294750c5693e3deac0cfa",
"name": "Mashhad",
"abb": "MS",
"population": 28
},
{
"_id": "65b2976346bd44878b4ae02c",
"name": "Tabriz",
"abb": "TB",
"population": 27
}
]
]
}
}
},
{
"$set": {
"cities": {
"$sortArray": {
"input": "$cities",
"sortBy": {
"_id": 1
}
}
}
}
},
{
"$set": {
"cities": {
"$slice": [
"$cities",
5
]
}
}
}
])
如果您正在寻找替代方法来检查当前文档中存在的提供的输入数组中的
_id
并仅在其不存在时添加,您可以使用以下查询:
db.collection.update({},
[
{
"$set": {
"cities": {
"$filter": {
"input": "$cities",
"as": "citiesItem",
"cond": {
"$ne": [
"$$citiesItem._id",
"65b294750c5693e3deac0cf8"
]
}
}
}
}
},
{
"$set": {
"cities": {
$reduce: {
input: [
{
"_id": "65b294750c5693e3deac0cfa",
"name": "Mashhad",
"abb": "MS",
"population": 28
},
{
"_id": "65b294750c5693e3deac0cfb",
"name": "Tabriz",
"abb": "TB",
"population": 27
}
],
initialValue: "$cities",
in: {
$concatArrays: [
"$$value",
{
$ifNull: [
{
$cond: {
if: {
$not: {
$in: [
"$$this._id",
"$cities._id"
]
}
},
then: [
"$$this"
],
else: null
}
},
[]
]
}
]
}
}
}
}
},
{
"$set": {
"cities": {
"$sortArray": {
"input": "$cities",
"sortBy": {
"_id": 1
}
}
}
}
},
{
"$set": {
"cities": {
"$slice": [
"$cities",
5
]
}
}
}
])