Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use findOneAndUpdate $pull and populate does not update or Populate

This is my data:

{
    "_id" : ObjectId("594e762b03cc52508686ceef"),
    "_members" : [ 
        "59527bd5e521801bf07eab98", 
        "594ecca47a699d1775c2a2db"
    ],
}

I want to delete 59527bd5e521801bf07eab98 from _members.

PostSchema:

const PostsSchema = new Schema({
    // ...
    _members: [{ type: Schema.Types.ObjectId, ref: 'user' }],
    // ...
})

UserSchema:

const UserSchema = new Schema({
    // ...
    posts : [{ type: Schema.Types.ObjectId, ref: 'post' }],
    // ...
})

Find And Update:

let id = mongoose.mongo.ObjectID(req.params.id)
let member = mongoose.mongo.ObjectID(req.params.member)
let populateQuery = [
    {path:'_members', select:'_id'}
]

Post.
findOneAndUpdate(
    {'_id' : id} ,
    {
       $pull: { '_members': { '_id': member }}
     },
     {'new': true}
).
populate(populateQuery).
exec(
  function (err, data) {
    res.send(data)
  }
);
like image 922
Milad Jafari Avatar asked Jan 03 '23 19:01

Milad Jafari


1 Answers

Interestingly "population" only seems to work by specifying the model option to .populate() in this case.

Also you only need supply the member directly to $pull as the _id is not actually a property, until "populated". The "_members" array is simply an array of ObjectId values within the Post itself. So supply the input directly:

Post.findOneAndUpdate(
  {'_id' : id} ,
  {
    $pull: { '_members': member }
  },
  {'new': true}
)
.populate({path:'_members', select:'_id', model: 'user' })
.exec(function(err,post) {
  if (err) throw err; // or something
  res.send(post)
})

Or with Promises

Post.findOneAndUpdate(
  {'_id' : id} ,
  {
    $pull: { '_members':  member }
  },
  {'new': true}
)
.populate({path:'_members', select:'_id', model: 'user' })
.then(post => res.send(post))
.catch(err => console.error(err));  // or something

Alternately call Model.populate() instead:

Post.findOneAndUpdate(
  {'_id' : id} ,
  {
    $pull: { '_members': member }
  },
  {'new': true}
).exec(function(err,post) {
  if (err) throw err; // or something

  Post.populate(post, {path:'_members', select:'_id', model: 'user' },function(err,post) {
    if (err) throw err; // or something
    res.send(post);   // now populated
  }
})

Or alternately using Promises:

Post.findOneAndUpdate(
  {'_id' : id} ,
  {
    $pull: { '_members': member }
  },
  {'new': true}
)
.then(post => Post.populate(post, {path:'_members', select:'_id', model: 'user' }))
.then(post => res.send(post))      // also populated
.catch(err => console.error(err))    // or whichever

Somewhat unsure about why you want to call .populate() when you are only asking to return the _id field which is already embedded in the document, but that's another case. With the model option the population actually takes place here.


As a self contained demonstration:

const async = require('async'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema;

mongoose.Promise = global.Promise;
mongoose.set('debug',true);
mongoose.connect('mongodb://localhost/test');

const relSchema = new Schema({ c: String });
const testSchema = new Schema({
  a: String,
  b: [{ type: Schema.Types.ObjectId, rel: 'Rel' }]
})

const Test = mongoose.model('Test', testSchema);
const Rel = mongoose.model('Rel', relSchema);

function log(data) {
  console.log(JSON.stringify(data, undefined, 2))
}

async.series(
  [
    (callback) =>
      async.each(mongoose.models,(model,callback) =>
        model.remove({},callback),callback),

    (callback) =>
      async.waterfall(
        [

          (callback) => Rel.create([{ c: 2 },{ c: 3 },{ c: 4 }],callback),


          (rels,callback) => Test.create({ a: 1, b: rels },(err,test) => {
            if (err) callback(err);
            log(test);
            callback(err,test.b.slice(1,3))
          }),

          (rels,calback) =>
            Test.findOneAndUpdate(
              { 'a': 1 },
              { '$pull': { 'b': rels[0] } },
              { 'new': true }
            )
            .populate({ path: 'b', model: 'Rel' })
            .exec((err,test) => {
              if (err) callback(err);
              log(test);
              callback(err);
            })

        ],
        callback
      )

  ],
  (err) => {
    if (err) throw err;
    mongoose.disconnect();
  }
)

And the output:

Mongoose: tests.remove({}, {})
Mongoose: rels.remove({}, {})
Mongoose: rels.insert({ c: '2', _id: ObjectId("595714579afd8860e56d2ec7"), __v: 0 })
Mongoose: rels.insert({ c: '3', _id: ObjectId("595714579afd8860e56d2ec8"), __v: 0 })
Mongoose: rels.insert({ c: '4', _id: ObjectId("595714579afd8860e56d2ec9"), __v: 0 })
Mongoose: tests.insert({ a: '1', _id: ObjectId("595714579afd8860e56d2eca"), b: [ ObjectId("595714579afd8860e56d2ec7"), ObjectId("595714579afd8860e56d2ec8"), ObjectId("595714579afd8860e56d2ec9") ], __v: 0 })
{
  "__v": 0,
  "a": "1",
  "_id": "595714579afd8860e56d2eca",
  "b": [
    "595714579afd8860e56d2ec7",
    "595714579afd8860e56d2ec8",
    "595714579afd8860e56d2ec9"
  ]
}
Mongoose: tests.findAndModify({ a: '1' }, [], { '$pull': { b: ObjectId("595714579afd8860e56d2ec8") } }, { new: true, upsert: false, remove: false, fields: {} })
Mongoose: rels.find({ _id: { '$in': [ ObjectId("595714579afd8860e56d2ec7"), ObjectId("595714579afd8860e56d2ec9") ] } }, { fields: {} })
{
  "_id": "595714579afd8860e56d2eca",
  "a": "1",
  "__v": 0,
  "b": [
    {
      "_id": "595714579afd8860e56d2ec7",
      "c": "2",
      "__v": 0
    },
    {
      "_id": "595714579afd8860e56d2ec9",
      "c": "4",
      "__v": 0
    }
  ]
}
like image 171
Neil Lunn Avatar answered Jan 06 '23 13:01

Neil Lunn