[英]Update documents in a collection if one of the document field value exists in array
I have a collection1
with collection1Column
and another column presentInArray1
,我有一个带有
collection1
的collection1Column
和另一列presentInArray1
,
--------------------------------------------
| collection1Column | presentInArray1 |
--------------------------------------------
| A | null |
| B | null |
| C | null |
| D | null |
| E | null |
--------------------------------------------
I have an array1
like this:我有一个这样的
array1
:
let array1 = ['A', 'C', 'D'];
Now I need to update collection1
's column presentInArray1
only if an exact match of collection1Column
is found in array1
.现在,仅当在
array1
中找到与collection1Column
完全匹配时,我才需要更新collection1
的列presentInArray1
。 In the example given - the matches are A , C & D .在给出的示例中 - 匹配项是A 、 C和D 。
One possible javascript solution I have thought is the following:我认为一种可能的 javascript 解决方案如下:
Get all the values from both the collection & array.从集合和数组中获取所有值。
Once the values are available, take up one by one values from collection1
's collection1Column
and iterate on array1
to find a match.一旦这些值可用,就从
collection1
的collection1Column
中一个一个地获取值,并在array1
上迭代以找到匹配项。
Once the match is found, will update the presentInArray1
with true
一旦找到匹配项,将用
true
更新presentInArray1
In this scenario - How do I update documents in the collection collection1
if one of the document field collection1Column
value exists in array1
?在这种情况下 - 如果
array1
中存在文档字段collection1Column
值之一,我如何更新集合collection1
中的文档?
Also, here is the code I have tried so far:另外,这是我迄今为止尝试过的代码:
collection1.update({
'collection1Column': {
$in: array1
}
}, {
$set: {
'presentInArray1': true
}
}, {
multi: true
},
function(err, result) {
if (err) throw err;
});
This also seems to be not working.这似乎也不起作用。 I'm not sure why:
我不确定为什么:
{"result":{"n":217,"nModified":0,"opTime":{"ts":"6833283007906840577","t":13},"electionId":"7fffffff000000000000000d","ok":1,"$clusterTime":{"clusterTime":"6833283007906840577","signature":{"hash":"W1KNoy0a9d2iTU3OiIa2jlc4y00=","keyId":"6803936209337843714"}},"operationTime":"6833283007906840577"},"connection":{"id":5,"host":"cluster0-shard-00-01-foobar.mongodb.net","port":27017},"modifiedCount":0,"upsertedId":null,"upsertedCount":0,"matchedCount":217,"n":217,"nModified":0,"opTime":{"ts":"6833283007906840577","t":13},"electionId":"7fffffff000000000000000d","ok":1,"$clusterTime":{"clusterTime":"6833283007906840577","signature":{"hash":"W1KNoy0a9d2iTU3OiIa2jlc4y00=","keyId":"6803936209337843714"}},"operationTime":"6833283007906840577"}
This looks very expensive in terms of memory and performance to me.就 memory 和性能而言,这看起来非常昂贵。
Please suggest!请建议!
In simple words, how to perform V Lookup in
MongoDB
?简单来说,如何在
MongoDB
中进行V 查找?
MongoDB server version: 4.2.6 MongoDB 服务器版本:4.2.6
collection1.update({
'array1': $collection1Column
}, {
$set: {
'presentInArray1': true
}
}, {
multi: true
},
function(err, result) {
if (err) throw err;
});
Assuming collection1Column
is not array type假设
collection1Column
不是数组类型
If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (eg , , etc.)如果该字段包含一个数组,则 $in 运算符选择其字段包含一个数组的文档,该数组包含至少一个与指定数组中的值匹配的元素(例如,等)
For more Details: https://docs.mongodb.com/manual/reference/operator/query/in/更多详情: https://docs.mongodb.com/manual/reference/operator/query/in/
I used updateMany
instead of update
and it works.我使用
updateMany
而不是update
并且它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.