1

Desired Behavior:

Delete a property from each object in an array of objects in all documents in a collection.

What I've Tried:

The docs show $unset is used for deleting object properties:

db.products.update(
   { sku: "unknown" },
   { $unset: { quantity: "", instock: "" } }
)

Another post gives an example of how to delete a nested property in multiple documents:

db.collectionName.update(
    { },
    { "$unset": { "values.727920": "" } },
    { "multi":true }
)

Source: https://stackoverflow.com/a/31384375

I had a look at:

https://docs.mongodb.com/manual/reference/operator/update/positional-all/
https://docs.mongodb.com/manual/reference/operator/update/positional-all/#positional-update-all

which gave the example:

db.students.update(
   { },
   { $inc: { "grades.$[]": 10 } },
   { multi: true }
)

So I tried the following and it seems to work:

db.my_collection.update(
    { },
    { "$unset" : { "array_of_objects.$[].weight": "" } },
    { "multi" : true }
)

Question:

Is this the correct way to:

delete each weight property in each object in array_of_objects in all documents

Schema:

{
    "_id": ObjectId("5d1d85aa00341124bc90d158"),
    "title": "hello 01",
    "array_of_objects": [
    {
        "color": "blue",
        "weight": "100",
        "date": "2019-07-04T11:12:59.356Z"
    },
    {
        "color": "blue",
        "weight": "100",
        "date": "2019-07-04T11:12:59.356Z"
    },
    {
        "color": "blue",
        "weight": "100",
        "date": "2019-07-04T11:12:59.356Z"
    }]
},
{
    "_id": ObjectId("5d1d85aa11341124bc90d158"),
    "title": "hello 02",
    "array_of_objects": [
    {
        "color": "blue",
        "weight": "100",
        "date": "2019-07-04T11:12:59.356Z"
    },
    {
        "color": "blue",
        "weight": "100",
        "date": "2019-07-04T11:12:59.356Z"
    },
    {
        "color": "blue",
        "weight": "100",
        "date": "2019-07-04T11:12:59.356Z"
    }]
},
{
    "_id": ObjectId("5d1d85aa22341124bc90d158"),
    "title": "hello 03",
    "array_of_objects": [
    {
        "color": "blue",
        "weight": "100",
        "date": "2019-07-04T11:12:59.356Z"
    },
    {
        "color": "blue",
        "weight": "100",
        "date": "2019-07-04T11:12:59.356Z"
    },
    {
        "color": "blue",
        "weight": "100",
        "date": "2019-07-04T11:12:59.356Z"
    }]
}
user1063287
  • 10,265
  • 25
  • 122
  • 218
  • Possible duplicate of [Remove a field from all elements in array in mongodb](https://stackoverflow.com/questions/19945924/remove-a-field-from-all-elements-in-array-in-mongodb) – Boaz Jul 04 '19 at 12:59
  • For reference, the accepted solution of the suggested duplicate is not helpful, however other answers there are, such as: https://stackoverflow.com/a/47836562 and in another post: https://stackoverflow.com/a/46054172 – user1063287 Jul 04 '19 at 13:06
  • If this is indeed a duplicate, please consider removing this post. – Boaz Jul 04 '19 at 13:07

0 Answers0