如果某些元素不存在或在mongoDB中进行更新,请按数组元素
我必须根据我从req.value
获得的字符串来更新文档中的数组。
My collection
[
{
"_id": 1,
"key": [
"1-value-1",
"1-value-2"
]
},
{
"_id": 2,
"key": [
"2-value-1",
"2-value-2"
]
}
]
I have another string newString
. Let's assume two conditions:
- when
req.value
is in the array in that case replace the array value withnewString
. - else
rew.value
不存在简单的推动newstring
数组中的。
例如:我有一个newsTring = 1-Value-3
和req.value = 1-Value-2
在这种情况下,替换阵列值1-Value-2到1-Value-3
。 否则,如果req.value
不在数组push newstring
值中。
这是 Collection 我想在上执行操作。
感谢您提前回答。
I have to update an array inside a document on the basis of a string which i get from the req.value
.
My collection
[
{
"_id": 1,
"key": [
"1-value-1",
"1-value-2"
]
},
{
"_id": 2,
"key": [
"2-value-1",
"2-value-2"
]
}
]
I have another string newString
.
Let's assume two conditions:
- when
req.value
is in the array in that case replace the array value withnewString
. - else
rew.value
is not present simple push thenewString
in the array.
Eg: I have a newString = 1-value-3
and req.value = 1-value-2
in that case replace array value 1-value-2 to 1-value-3
.
Else if req.value
isn't in array push newString
value to array.
This is the collection on which i want to perform operations on.
Thanks for answering in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用管道更新
查询
1-Value-2存在
更改为1-value-3
playmongo (已更新,存在)
playmongo
(检查是否存在),
如果query1空结果发送此(按末尾按)
else发送此(设置以替换旧值)
Update with pipeline
Query
1-value-2 exists
change to1-value-3
Playmongo(update,exists)
Playmongo(push(concat), missing)
Update operators
Query1(check if exists)
If query1 empty result send this(push at the end)
else send this (set to replace the old value)