英文:
Accessing $map variable in $cond
问题
以下是您提供的代码的翻译部分:
我有一些存储在MongoDB集合中的数据,看起来像这样:
```javascript
db.test.insertOne(
{ "interactions": [
{
data: "keep",
prompt: "prompt 1"
},
{
other: "keep",
prompt: "prompt 2"
},
{
field: "no prompt"
}
]}
)
我想遍历所有的interactions
,并将prompts
设置为包含当前prompt
值的数组。类似于这样:
{
"interactions": [
{
"data": "keep",
"prompt": "prompt 1",
"prompts": [ "prompt 1" ]
},
{
"other": "keep",
"prompt": "prompt 2",
"prompts": [ "prompt 2" ]
},
{
"field": "no prompt"
}
]
}
我一直在尝试使用带有聚合管道的updateMany()
,但出现了一个我不理解的错误。这是我的更新操作:
db.test.updateMany(
{},
[{
$set: {
interactions: {
$map: {
input: "$interactions",
as: "interaction",
in: {
$mergeObjects: [
"$$interaction",
{
$cond: {
if: { "$$interaction.prompt": { $exists: true } },
then: {
prompts: [ "$$interaction.prompt" ]
},
else: {}
}
}
]
}
}
}
}
}]
)
运行此操作时,我收到错误消息:
无法识别的表达式'$$interaction.prompt'。
运行不带$cond
的更新操作可以正常工作:
db.test.updateMany(
{},
[{
$set: {
interactions: {
$map: {
input: "$interactions",
as: "interaction",
in: {
$mergeObjects: [
"$$interaction",
{
prompts: [ "$$interaction.prompt" ]
}
]
}
}
}
}
}]
)
但在该操作之后,没有prompt
的数组元素会具有一个包含null
值的数组:
{
"interactions": [
{
"data": "keep",
"prompt": "prompt 1",
"prompts": ["prompt 1"]
},
{
"other": "keep",
"prompt": "prompt 2",
"prompts": ["prompt 2"]
},
{
"field": "no prompt",
"prompts": [null]
}
]
}
我不希望在没有prompt
的元素上设置prompts
。
那么为什么我无法在$cond
中访问$$interaction.prompt
?
或者,如果有更好的方法来实现我想要做的事情,请告诉我。
<details>
<summary>英文:</summary>
I have some data in a MongoDB collection that looks something like this:
```javascript
db.test.insertOne(
{ "interactions": [
{
data: "keep",
prompt: "prompt 1"
},
{
other: "keep",
prompt: "prompt 2"
},
{
field: "no prompt"
}
]}
)
I want to iterate over all the interactions
, and set prompts
to an array containing the current value of prompt
. Something like this:
{
"interactions": [
{
"data": "keep",
"prompt": "prompt 1",
"prompts": [ "prompt 1" ]
},
{
"other": "keep",
"prompt": "prompt 2",
"prompts": [ "prompt 2" ]
},
{
"field": "no prompt"
}
]
}
I've been trying to do a updateMany()
with an aggregation pipeline, but am getting an error that I don't understand. This is my update:
db.test.updateMany(
{},
[{
$set: {
interactions: {
$map: {
input: "$interactions",
as: "interaction",
in: {
$mergeObjects: [
"$$interaction",
{
$cond: {
if: { "$$interaction.prompt": { $exists: true } },
then: {
prompts: [ "$$interaction.prompt" ]
},
else: {}
}
}
]
}
}
}
}
}]
)
Running this I get an error:
> Unrecognized expression '$$interaction.prompt'.
Running the update without the $cond
works:
db.test.updateMany(
{},
[{
$set: {
interactions: {
$map: {
input: "$interactions",
as: "interaction",
in: {
$mergeObjects: [
"$$interaction",
{
prompts: [ "$$interaction.prompt" ]
}
]
}
}
}
}
}]
)
but after that operation, the array element that didn't have a prompt
has an array with a null
value:
{
"interactions": [
{
"data": "keep",
"prompt": "prompt 1",
"prompts": ["prompt 1"]
},
{
"other": "keep",
"prompt": "prompt 2",
"prompts": ["prompt 2"]
},
{
"field": "no prompt",
"prompts": [null]
}
]
}
I don't want prompts
set on an element that didn't have prompt
.
So why can't I access $$interactions.prompt
within the $cond
?
Alternatively, if there's a better way to accomplish what I want to do, please let me know.
答案1
得分: 3
在聚合管道中,你不能使用$exists
运算符。
而且,你必须以$
运算符开头,而不是以变量$$
开头。
相反,你需要检查$$interaction.prompt
是否既不是undefined
也不是null
。
英文:
In the aggregation pipeline, you can't use the $exists
operator.
And you must start with the $
operator but not with variable $$
.
Instead, you check whether $$interaction.prompt
is neither undefined
nor null
.
db.collection.updateMany({},
[
{
$set: {
interactions: {
$map: {
input: "$interactions",
as: "interaction",
in: {
$mergeObjects: [
"$$interaction",
{
$cond: {
if: {
$not: {
$in: [
"$$interaction.prompt",
[
undefined,
null
]
]
}
},
then: {
prompts: [
"$$interaction.prompt"
]
},
else: {}
}
}
]
}
}
}
}
}
])
答案2
得分: 0
以下是您要翻译的内容:
For some reason, using $not
and $in
didn't work in my database, although it did in the Playground. After finding an alternative here: https://stackoverflow.com/a/25515046/1303158, my final solution is:
db.collection.update({},
[
{
$set: {
interactions: {
$map: {
input: "$interactions",
as: "interaction",
in: {
$mergeObjects: [
"$$interaction",
{
$cond: {
if: {
$gt: [
"$$interaction.prompt",
null
]
},
then: {
prompts: [
"$$interaction.prompt"
]
},
else: {}
}
}
]
}
}
}
}
}
],
{
multi: true
})
英文:
For some reason, using $not
and $in
didn't work in my database, although it did in the Playground. After finding an alternative here: https://stackoverflow.com/a/25515046/1303158, my final solution is:
db.collection.update({},
[
{
$set: {
interactions: {
$map: {
input: "$interactions",
as: "interaction",
in: {
$mergeObjects: [
"$$interaction",
{
$cond: {
if: {
$gt: [
"$$interaction.prompt",
null
]
},
then: {
prompts: [
"$$interaction.prompt"
]
},
else: {}
}
}
]
}
}
}
}
}
],
{
multi: true
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论