简体   繁体   English

如果数组中存在文档字段值之一,则更新集合中的文档

[英]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 ,我有一个带有collection1collection1Column和另一列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 .在给出的示例中 - 匹配项是ACD

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.一旦这些值可用,就从collection1collection1Column中一个一个地获取值,并在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.

相关问题 Firebase Firestore:如何更新或访问和更新 map、数组、文档、集合中的字段值 - Firebase Firestore: How to update or access and update a field value, in a map, in an array, in a document, that is in a collection 如何使用另一个集合中的字段值更新Mongodb集合中的文档 - How to update document in Mongodb collection with a field value from another collection 查询特定数组字段中是否存在值 - 集合中的所有文档 - Query for existence of a value in a specific array field - All documents within a collection 获取具有特定嵌套字段值的所有文档,无一个文档 - Get all documents with specific nested field value, sans one document 将一个新字段添加到集合的所有文档中,将文档字段中的值添加到 MongoDB (Mongoose) 中,记录为 300K+ - Add a new field to all documents of a collection with the value from the document field into MongoDB (Mongoose) with records of 300K+ 循环虽然集合中的所有文档和每个文档中的数组都将数组值与项目匹配 - Looping though all documents in the collection and an array in each document to match array value to the project Meteor.js遍历数组以检查集合文档中是否已存在值 - Meteor.js Loop over an array to check if value already exists in collection documents 检查多个文档中是否存在值并返回该文档 - Checking if value exists across multiple documents and returning that document 集合中嵌入文档的集合更新属性 - collection update property of embedded document in array 在一个字段中按数组查找mongodb中的文档? - finding documents in mongodb by array for one field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM