A
A
Anastasia2017-11-14 17:51:35
JavaScript
Anastasia, 2017-11-14 17:51:35

How to add a field to an array that is in an object (mongoDB)?

here is the db:

{
    "_id" : "NGhp5Ad8cTvcQaWjf",
    "profile" : {
        "name" : "dsfsdf",
        "favorites" : [ 
        ]
    }
}

You need to add a field to the "favorites" array.
The following query fails because "Cannot apply $push modifier to non-array for field 'profile' [409]":
Meteor.users.update({"_id" : Meteor.userId()}, {$push: {"profile" : {"favorites": instance.data.userId}}})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
Urukhayy, 2017-11-14
@lastees

The $push property takes as its value an object with a property corresponding to the name of the array. In your case -- $push takes an object with a "profile" property that points to a non-array field. Need like this:

$push: {
    "profile.favorites": ......
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question