MongoDB - using $ in with $ pull on update only affects one record, not all specified in $ in

Ok, this can be a little tricky to visualize without posting the example document specification in question, but I'm not sure if this is even relevant in this case.

Essentially what I'm trying to do here is $pull

throwing out a specific part of the document from the list of post IDs. Here's an example (we're using PyMongo to interact with the database):

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)

      

What I'm trying to do here is remove the "activity" from the "poster" feeds that participated in the "activity" mentioned. So, I take the IDs of all the "posters" from the "action" and then use that list in the $in

operator sentence update

. Then I try to execute the $pull

"posters" from the corresponding parts of the subwoofer.

The problem is that it only applies this change to the first item in the list , or to the first "poster". The syntax is correct as far as I know, but it might be an edge case.

I can be a lazy list iterator and apply this operator to each identifier separately, but I would rather dump it to the DB if it supports it.

As always, thank you very much!

Edit:

Set Mongod to its maximum length -vvvvvv

and pull the following entry from the logs that correspond to the PyMongo call:

update posters query: { _id: { $in: [ "1", "10", "18537", "19778", "20137", "20967", "4", "54", "5835", "9" ] } } update: { $pull: { feed: { _id: "4020800", object: "activity" } } }

      

Then I went into the shell and did a manual update:

db.posters.update({ _id: { $in: [ "1", "10", "18537", "19778", "20137", "20967", "4", "54", "5835", "9" ] } }, { $pull: { feed: { _id: "4020800", object: "activity" } } })

      

I checked the affected entries inside the shell and it worked fine. I think it might be an edge case or a limitation in PyMongo bindings. Anyway, if someone can shed some light on this, I would be very happy!

+3


source to share


1 answer


When updating multiple documents in a single update command, you must set the multiflag value to true. As shown in the pymongo example, it is already set to true. But in the magazine you pulled out, there is no sign of the ambiguous flag. I believe that is the problem.

Check out the below example done in mongo shell with multiple

> db.posters.insert({_id:1,feed:[{_id:"4020800", object: "activity"}]})
> db.posters.insert({_id:10,feed:[{_id:"4020800", object: "activity"}]})
> db.posters.insert({_id:101,feed:[{_id:"4020800", object: "activity"}]})
> db.posters.find()
{ "_id" : 1, "feed" : [ { "_id" : "4020800", "object" : "activity" } ] }
{ "_id" : 10, "feed" : [ { "_id" : "4020800", "object" : "activity" } ] }
{ "_id" : 101, "feed" : [ { "_id" : "4020800", "object" : "activity" } ] }

      

and

> db.posters.update({ _id: { $in: [ 1, 10]}}, { $pull: { feed: { _id: "4020800", object: "activity" } } },false,true)
> db.posters.find()
{ "_id" : 1, "feed" : [ ] }
{ "_id" : 10, "feed" : [ ] }
{ "_id" : 101, "feed" : [ { "_id" : "4020800", "object" : "activity" } ] }

      



It works great. So I believe there is a problem in the driver, not mongo.

Also I don't believe it works great in your last example either.

Then I went into the shell and did a manual update:

db.posters.update({ _id: { $in: [ "1", "10", "18537", "19778", "20137", "20967", "4", "54", "5835", "9" ] } }, { $pull: { feed: {

      

_id: "4020800", object: "activity"}}})

I checked the affected entries inside the shell and it worked great.

Without the multi flag, it only updates the first element. below sample

> db.posters.update({ _id: { $in: [ 1, 10]}}, { $pull: { feed: { _id: "4020800", object: "activity" } } })
> db.posters.find()
{ "_id" : 1, "feed" : [ ] }
{ "_id" : 10, "feed" : [ { "_id" : "4020800", "object" : "activity" } ] }
{ "_id" : 101, "feed" : [ { "_id" : "4020800", "object" : "activity" } ] }

      

+2


source







All Articles