0

I have a collection1 with collection1Column and another column presentInArray1,

--------------------------------------------
| collection1Column | presentInArray1      |
--------------------------------------------
| A                 | null                 |
| B                 | null                 |
| C                 | null                 |
| D                 | null                 |
| E                 | null                 |
--------------------------------------------

I have an array1 like this:

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. In the example given - the matches are A, C & D.

One possible javascript solution I have thought is the following:

  • 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.

  • Once the match is found, will update the presentInArray1 with true

In this scenario - How do I update documents in the collection collection1 if one of the document field collection1Column value exists in array1?

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.

Please suggest!

In simple words, how to perform V Lookup in MongoDB?

MongoDB server version: 4.2.6

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • 1
    **1)** Did you try `$in` https://stackoverflow.com/questions/18148166/find-document-with-array-that-contains-a-specific-value ? **2)** What is the value that you wanted to update in `presentInArray1` for docs which match with `A`,`C`,`D`? **3)** what is your DB version ? – whoami - fakeFaceTrueSoul Jun 01 '20 at 04:21

2 Answers2

1
collection1.update({
    'array1': $collection1Column
  }, {
    $set: {
      'presentInArray1': true
    }
  }, {
    multi: true
  },
  function(err, result) {
    if (err) throw err;
  });

Assuming collection1Column is not array type

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 (e.g. , , etc.)

For more Details: https://docs.mongodb.com/manual/reference/operator/query/in/

ashutosh
  • 68
  • 7
0

I used updateMany instead of update and it works.

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219