Pull not working for multiple documents

Hello,

I have the following query that is only $pulling from one document. Document structure is like so:

{
  ...document properties
  cachedDocs: [{
    _id:"adlfjalfefwehfwl",
    name: "bob"
  }]
}

I have embedded documents in the array that have an _id property. I need to remove some of them and add some of them. The reason I store them there is because I’m basically caching them in one spot, because they change infrequently, and I don’t want to grab them from all the associated documents. In the course of changing them, I have found that $pull only works on one document. If two documents have the same item in the cachedDocs array, the cached document is only removed from the first document’s array, but left intact on the second. Am I doing something wrong in the query?

collection.direct.update({ 'parents': doc._id },
  { $pull: { cachedDocs: { _id: { $in: cachedDocsToRemoveIds } } } });

//console.log to prove successful update
var result = collection.direct.find({ parents: doc._id,
  "cachedDocs._id": { $in: cachedDocsToRemoveIds } }).fetch();
console.log(JSON.stringify(result, null, "\t"));

If this query ran correctly, the console.log statement should print out an empty array. Unfortunately, the second document shows up. I deleted the first document manually, and sure enough, the second one got edited correctly when it was the only one there.

I think you’re just missing the multi property?

https://docs.mongodb.com/manual/reference/method/db.collection.update/#example-update-multi

For some reason I thought you didn’t have to do that with meteor. I don’t know why. Anyways, I added it and it works! Thanks vigorwebsolutions!

1 Like