Meteor collection.update with $pull always returns 1

I have a collection with an array field. I have code to update a single document in this collection. The update needs to remove a single element from the array field and I use $pull. I expect Meteor’s Mongo.Collection.update to return 0, if the array element I specify does not exist in the specified document, but instead I see it returns one.

I issue the same command in mongo shell and do see the expected WriteResult.nModified.

Mongo shell - assume the referenced cars document does NOT have ‘foobar’ in it’s fileIds array property.

meteor:PRIMARY> print(db.cars.update({ _id: 'K9RKyTCzaAp4YipEF' }, { $pull: {fileIds: 'foobar'} }));
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

Same thing, done on Meteor server-side always returns me 1.

var nModified = Cars.update(this.id, {
    $pull: { fileIds: 'foobar' }
});
logger.info("Removed file count", nModified); >>>>>> Always returns 1

I’m not using SimpleSchema for this collection, although I’m using it for some other collections.

Thanks Rags

1 Like

Modify your query to select only those documents which this value can be pulled from.

var nModified = Cars.update({
    _id: this.id,
    fileIds: 'foobar'
  }, {
    $pull: { fileIds: 'foobar' }
  });

Thank you for your suggestion aedm. But this seems like a workaround. What are your thoughts on why this difference between Mongo and Meteor exists in the first place?

Lol, wrong paste!

Sorry!