Z
Z
Zhanna_K2020-10-09 14:00:39
MongoDB
Zhanna_K, 2020-10-09 14:00:39

How to prevent duplication in an array?

Is it possible to prevent the same IDs from being added to the array? Added { unique: true, dropDups: true } , but it doesn't help. Perhaps I am passing this option incorrectly.
Tell me, please, what to change?

Project.updateOne(
      { _id: req.params.projectId },
      {
        $push: { developers: req.body.developer.devId },
      },
      { unique: true, dropDups: true }
    );

5f8042bb967d4042626204.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2020-10-12
@zoonman

$addToSet

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question