MongoDB & Meteor - query for nested array does not work, no error occurs

I am trying to insert data into a nested array inside a Mongo collection. I followed Mongo's documentation, http://docs.mongodb.org/manual/reference/operator/update/positional/ but didn't have time to push the array. Errors or exceptions are not thrown and the syntax looks correct ...

In this example I am trying to update buyer.boards

called "Board One" by pushing a new row into the array idArr

. Is there something wrong with my request?

Mongo collection

// User Document from Meteor.users Collection:
{
    _id: 'userIdqwerty',
    buyer: {
        boards: [
            {
                title: 'Board One',
                idArr: ['id123', 'id456', 'id678']
            },
            {
                title: 'Board Two',
                idArr: ['idABC']
            },
            {
                title: 'Board Three',
                idArr: ['id12345678', 'idqwertyuu']
            },
        ]
    };
}

      

Javascript

var options = {
    boardTitle: 'Board One',
    newId: 'idZjodFsp',
    userId: 'userIdqwerty'
};

Meteor.users.update(
    { 
        _id:options.userId, 
        'buyer.boards.$.title':options.boardTitle 
    },
    { $push: { 
        'buyer.boards.$.idArr':options.newId }
    }
);

      

+3


source to share


1 answer


Remove the positioning operator ( $

) from the query

function parameter update

.

Meteor.users.update(
    { 
        _id:options.userId, 
        'buyer.boards.title':options.boardTitle 
    },
    { $push: { 
        'buyer.boards.$.idArr':options.newId }
    }
);

      

From the docs:



db.collection.update(
   { <array>: value ... },
   { <update operator>: { "<array>.$" : value } }
)

      

The positional operator must be used in a parameter update

and not in a parameter query

. This only updates the first object boards

that has a matching one title

.

+3


source







All Articles