Unset not working on updateMany

I want to find all documents in collection my_collection, that contain the nested field “some.deep.field”, and unset that field. I am trying the following in mongosh (in Compass, Atlas):

db.my_collection.updateMany(
[
  {
    $match:
      {
        "some.deep.field":
          {
            $exists: true,
          },
      },
  },
  {
    $unset:
      "some.deep.field",
  },
]
)

When I do that, it returns
{
acknowledged: true,
insertedId: null,
matchedCount: 192,
modifiedCount: 0,
upsertedCount: 0
}

But nothing is modified.

What am I doing wrong??

Hello @Victor_Grazi,

First of all, I don’t how your query works, because the syntax is wrong and this will throw an error.

Check out the update with aggregation pipeline syntax from this documentation, it’s not really hard to understand.

The problem with your query:

  • You missed the query filter and directly passed the aggregation pipeline in the first argument.
  • The $match stage is not a supported stage in the update with aggregation pipeline, because you can filter by passing the query filter in the first argument same as a regular update query.
2 Likes

While you are on the forum, please take the time to provide closure on the following thread:

Looking at the link you provided, I see the following example:

db.students.updateMany(
   { },
   [
      { $set: { comments: [ "$commentsSemester1", "$commentsSemester2" ], lastUpdate: "$$NOW" } },
      { $unset: [ "commentsSemester1", "commentsSemester2" ] }
   ]
)

Let’s try it that way then:

db.my_collection.updateMany(
   { },
   [
      { $unset: [ "some.deep.field" ] }
   ]
)

Woops, still no good…

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 0,
  modifiedCount: 0,
  upsertedCount: 0
}

What I am trying to do is remove the some.deep.field from any document that has it.
Can anyone tell me the proper syntax?

Can you please share the example document?

The document is something like this

{
  "_id": "123",
  "some": {
    "deep": [
      "field",
      "field1"
    ]
  }
}

Ahh, this does not require an update with the aggregation pipeline, You can use a regular query with the $pull operator, because “field” is not a property it’s a value/element of an array.

db.my_collection.updateMany({
  "some.deep": "field"
},
{
  $pull: {
    "some.deep": "field"
  }
})

[quote=“Victor_Grazi, post:6, topic:275486”]
Sorry, I transcribed it incorrectly, I am trying to remove a field from an object in an array. This is more accurate:

{
  "_id": "123",
  "some": [
    "deep",
    {
      "field":{
      "x": "x"
    },
      "other_field": {
        "y": "y"
      }
    }
  ]
}

I am trying to remove any element called “field” from any object called “deep” in the “some” array

Could you please stop redacting your documents? The last document you shared is not valid JSON.

Please provide real documents from your collection.

Wish I could, where I work they fire you for less. But my json checker shows the document is in fact valid. What do you see wrong?

Sorry. I must have cut-n-past wrongly the first time. It is indeed valid JSON.

However, it does not match the description:

There is no object called deep. There is however a string value “deep” as the first element of the array. The field named field is one of the 2 fields of the second element of the array.

Sorry, transcription errors. This one is pretty close. I am trying to modify every collection that has
{“globalSearchConfig.aggregationStages.search.compound.should.autocomplete.fuzzy”:{$exists:true}}, to unset the field called “fuzzy” from any object called “autocomplete” in the “should” array that has it

{
	"_id": "123",
	"searchContext": "group",
	"searchIndex": "index__group",
	"aggregationStages": {
	  "search": {
		"compound": {
		  "should": [
			{
			  "autocomplete": {
				"path": "name",
				"fuzzy": {
				  "maxEdits": 1
				}
			  }
			},
			{
			  "autocomplete": {
				"query": "%VALUE%",
				"path": "desc",
				"fuzzy": {
				  "maxEdits": 1
				}
			  }
			}
		  ],
		  "minimumShouldMatch": 1
		}
	  },

	  "sysAuditFields": {
		"createdBy": "DBA",
		"updatedTs": {
		  "$date": "2023-03-17T02:53:00.792Z"
		},
		"createdTs": {
		  "$date": "2020-06-16T19:02:20.814Z"
		}
	  }
	}
}

Meant to say I am trying to modify every document

Why don’t they let you edit these postings after you make them???

Worked it out:

db.PXDatalakeCollectionConfig.updateMany(
  { "globalSearchConfig.aggregationStages.search.compound.should.autocomplete.fuzzy": { $exists: true } },
  { $unset: { "globalSearchConfig.aggregationStages.search.compound.should.$[].autocomplete.fuzzy": "" } }

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.