Kunok Kunok - 1 year ago 68
Node.js Question

Unable to push data into current object inside mongo database

I am using mongodb native and Node.js 6.5.0.

I have user object in mongodb which is structured as:

"_id" : ObjectId("57d7d294d96a73d128c46db9"),
"id" : "105862592064",
"labels" : [


I have a loop (for each user found) get data from API and then push it into object attribute of array type. Where user id
and data to push is

This is my code:


It doesn't return any error, neither it does update objects. What have I done wrong?

Answer Source

$push is for pushing a single element to an array. Use $push together with $each to push multiple elements. Also, quotes around the object labels should not be neccessary:

      { id:user.id },
      { $push: { labels: { $each: resp.labels } } }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download