Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-24340

positional $ operator update fails without match from query document

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • None
    • Querying
    • None
    • ALL
    • Hide

      Given docs with this structure (from docs created using MongoEngine {{EmbeddedDocument}}s ):

      {
              ...
      	"status_history" : [
      		{
      			"_types" : [
      				"EntityStatus"
      			],
                              ... 
      

      Running this on the console:

      > db.entities.update({}, {'$unset': {'status_history.$._types': true}}, {'multi': true})
      WriteResult({
      	"nMatched" : 0,
      	"nUpserted" : 0,
      	"nModified" : 0,
      	"writeError" : {
      		"code" : 16837,
      		"errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: status_history.$._types"
      	}
      })
      > db.entities.update({}, {'$unset': {'status_history.0._types': true}}, {'multi': true})
      WriteResult({ "nMatched" : 1570, "nUpserted" : 0, "nModified" : 1179 })
      

      Show
      Given docs with this structure (from docs created using MongoEngine {{EmbeddedDocument}}s ): { ... "status_history" : [ { "_types" : [ "EntityStatus" ], ... Running this on the console: > db.entities.update({}, {'$unset': {'status_history.$._types': true}}, {'multi': true}) WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 16837, "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: status_history.$._types" } }) > db.entities.update({}, {'$unset': {'status_history.0._types': true}}, {'multi': true}) WriteResult({ "nMatched" : 1570, "nUpserted" : 0, "nModified" : 1179 })

    Description

      Original summary

      $unset with positional $ fails to find nested field

      Original Description

      In a database with documents with a list of nested documents $unset does not match all elements in that list as expected.

      A query like this

       db.entities.update({}, {'$unset': {'status_history.$._types': true}}, {'multi': true}) 

      should completely remove all `_types` entries from all `status_history` items from all `entities` docs (or at least not fail).

      ...although the mongo manual at https://docs.mongodb.com/manual/reference/operator/update/positional/#unsets states "When used with the $unset operator, the positional $ operator does not remove the matching element from the array but rather sets it to null." which sounds like a flaw, not a desirable feature, but that's a different issue.

      Attachments

        Activity

          People

            kelsey.schubert@mongodb.com Kelsey Schubert
            codelikethis Alex Chaffee
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: