Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB - Using $in with $pull on an update only affects a single record instead of all specified within $in
    text
    copied!<p>Alright, this might be a little difficult to visualize without posting an example of the document spec in question, but I'm not sure if it's even relevant in this case.</p> <p>Essentially, what I'm trying to do here, is <code>$pull</code> out a specific part of a document from a list of record IDs. Here is an example (we're using PyMongo to interact with the database):</p> <pre><code>distinct_poster_ids = self._db.activities.find({"_id": activity_id}).distinct("feed.poster_id") if distinct_poster_ids: document = { "$pull": { "feed": { "_id": activity_id, "object": "activity" } } } self._db.posters.update({"_id": {"$in": distinct_poster_ids}}, document, multi=True) </code></pre> <p>What I'm trying to do here is remove an "activity" from the feeds of "posters" who have participated in said "activity". So, I grab the IDs of all "posters" from the "activity" and then use that list within the <code>$in</code> clause of the <code>update</code> statement. I then attempt to <code>$pull</code> out the relevant parts of the "posters" feed subdocument.</p> <p><strong>The problem here is that it's only applying this change to the first item in the list</strong>, or, the first "poster". As far as I know, the syntax is correct, but this might be an edge case.</p> <p>I can be lazy an iterate through the list and apply this statement to each ID individually, but <strong>I'd rather offload this to the DB if it supports it.</strong></p> <p>As always, thank you so much!</p> <p>Edit:</p> <p>Set Mongod to maximum verbosity <code>-vvvvvv</code> and pulled the following entry from the logs that correspond with the PyMongo call:</p> <pre><code>update posters query: { _id: { $in: [ "1", "10", "18537", "19778", "20137", "20967", "4", "54", "5835", "9" ] } } update: { $pull: { feed: { _id: "4020800", object: "activity" } } } </code></pre> <p>I then logged into the shell and executed the update manually:</p> <pre><code>db.posters.update({ _id: { $in: [ "1", "10", "18537", "19778", "20137", "20967", "4", "54", "5835", "9" ] } }, { $pull: { feed: { _id: "4020800", object: "activity" } } }) </code></pre> <p>I checked the affected records within the shell and it worked perfectly. I'm thinking this might be an edge case or limitation within the PyMongo bindings. At any rate, if anyone can shed some light on this, I would quite happy!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload