68

I'm using node.js and Mongo.db (I'm newly on Mongo). I have a document like this:

Tag : {
   name: string,
   videoIDs: array
}

The idea is, the server receives a JSON like

JSON: { 
    name: "sport",
    videoId: "34f54e34c"
}

with this JSON, it has to find the tag with the same name and check if in the array it has the videoId, if not, insert it to the array.

How can I check the array and append data?

Albert Lazaro de Lara
  • 2,540
  • 6
  • 26
  • 41

3 Answers3

170

You can use $addToSet operator to check exist before append element into array.

db.tags.update(
    {name: 'sport'},
    {$addToSet: { videoIDs: "34f54e34c" } }
);

In this update statement example, mongoDB will find the TAG document which matches name == sport, and then check whether the videoIDs array contains 34f54e34c. If not, append it to the array.

Detail usage of $addToSet please read here.

Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
yellowB
  • 2,900
  • 1
  • 16
  • 19
  • 1
    @albert , yes you can insert one element. "videoIDs" is an array, and you want to insert string "34f54e34c" into it if not exists, right? – yellowB Aug 16 '16 at 09:26
  • It works, it add new ids if there aren't, avoiding duplicates – Albert Lazaro de Lara Aug 18 '16 at 06:47
  • 3
    What if the field 'videoIDs' doesn't exist to start with? – Gaurav Ojha Apr 25 '17 at 12:29
  • 1
    @GauravOjha, if the field 'videoIDs' doesn't exist before executing the update statement, it will be created automatically – yellowB Aug 30 '17 at 03:22
  • can any one tell when $addtoSet got introduced, I mean from which mongodb version.? – piyushmandovra Feb 01 '18 at 12:49
  • I also want to increment the count if the add to set operation is performed – Shantanu Madane Aug 07 '18 at 16:18
  • @ShantanuMadane, pls refer to `$inc`. https://docs.mongodb.com/manual/reference/operator/update/inc/ – yellowB Aug 08 '18 at 06:47
  • @yellowB I actually want to increment only if $add to set adds element to an array. How can I achieve that? I know I can write two different queries but I want to do it in a single query. Is this possible? – Shantanu Madane Aug 10 '18 at 05:17
  • @ShantanuMadane,yes you can put multiple update operations into single statement,e.g. {$addToSet : { videoId : "34f54e34c"}, $inc: {count: 1}} – yellowB Aug 22 '18 at 08:52
  • does `$addToSet` create the array if it's not yet to exist? – Gobliins Aug 13 '19 at 10:37
  • An array is typically expected to have a deterministic order, but $addToSet does not guarantee a particular ordering of elements in the modified set (see https://docs.mongodb.com/manual/reference/operator/update/addToSet/#behavior) – Claudijo May 18 '20 at 11:03
8

I didn't test it, but you can try something like:

yourDb.find({ name: "sport", 
              videoIDs: { $in: ["34f54e34c"] } })
      .update({$addToSet: { videoIDs: "34f54e34c" }});

If you need how: MongoDb in nodeJs implementation, you can start with:

http://jsfiddle.net/1ku0z1a1/

Maria Maldini
  • 473
  • 1
  • 4
  • 7
7

$addToSet operator check for the empty or existing array in document.

 db.col_name.update({name :"name_for_match"},{$addToSet :  { videoId : "video_id_value"}})
turivishal
  • 34,368
  • 7
  • 36
  • 59
Vora Ankit
  • 682
  • 5
  • 8