William Falcon
William Falcon

Reputation: 9813

Mongo $push $position fails

For some reason, the $position is not doing anything in this code. Any thoughts?:

Parent.findByIdAndUpdate(
        id,
        {$push: {"wishList": item, $position: 0}},
        {safe: true, upsert: true},
        function(err, model) {
          response.send("1");
        }
        );

Here is the schema:

/**
Defines Parent schema for mongo DB
*/
module.exports = {
    name        : String,
    dateAdded   : Date,
    plHash      : String,
    items       : [{
        author      : String,
        name        : String,
        dateAdded   : Date
    }],
    wishList        : [{
        author      : String,
        name        : String,
        dateAdded   : Date
    }],

};

Upvotes: 0

Views: 174

Answers (1)

chridam
chridam

Reputation: 103305

Following the docs

To use the $position modifier, it must appear with the $each modifier.

You could try adding the $each modifier in your update:

Parent.findByIdAndUpdate(id,
{
  $push: {
    wishList: {
       $each: [item], // Assuming item = {author:"foo", name:"bar", dateAdded:date }
       $position: 0
     }
  }
}, {safe: true, upsert: true}, function(err, model){
    response.send("1");
});

Upvotes: 1

Related Questions