3

I have the following structure for my document:

{
    "Name": "Test",
    "FieldsCollection": [{
        "GroupName": "Group",
        "Fields": [{
            "FieldName": "ABC",
            "Fields": {
                "item1": "value1",
                "item2": "value2"
            }
        }]
    }]
}

and I need to change it to be as follows:

{
    "Name": "Test",
    "FieldsCollection": [{
        "GroupName": "Group",
        "Fields": [{
            "FieldName": "ABC",
            "item1": "value1",
            "item2": "value2"
        }]
    }]
}

Assuming that the values for "item1": "value1","item2": "value2" are constant for all the documents in my collection, I thought I could just remove "FieldsCollection.Fields.Fields" and add "item1": "value1","item2": "value2"

I tried the following query:

db.getCollection('Devices').update(
    {"FieldsCollection.Fields.FieldName":"ABC"},
    {$unset: {"FieldsCollection.Fields.Fields":1}},
    {multi:true}
)

But it didn't work.

What query can I use to perform this change?

5
  • docs.mongodb.com/v3.6/reference/operator/update/positional/… Commented Jul 11, 2018 at 12:20
  • Your fields.fields is an object and not array ? Commented Jul 11, 2018 at 12:23
  • @AlexBlex, Does it mean it is not possible? I provided the query I used, can you help adjust it? Commented Jul 11, 2018 at 12:25
  • Your query for removing is not correct Commented Jul 11, 2018 at 12:25
  • @PrajvalM, If it was I wouldn't be asking this question... Do you know why? Commented Jul 11, 2018 at 12:29

2 Answers 2

8

Use positional $[all] (first level) & positional [<identifier>] (second level) in 3.6

Something like

db.getCollection('Devices').update(
    {"FieldsCollection.Fields.FieldName":"ABC"},
    {$unset: {"FieldsCollection.$[].Fields.$[f].Fields":1}, 
     $set:{"FieldsCollection.$[].Fields.$[f].item1":"value1",
           "FieldsCollection.$[].Fields.$[f].item2":"value2"}
    },  
    {arrayFilters: [{ "f.FieldName":"ABC"} ],multi:true }
)
Sign up to request clarification or add additional context in comments.

9 Comments

This gives the following error: Failed to execute script. Error: Fourth argument must be empty when specifying upsert and multi with an object. : DBCollection.prototype._parseUpdate@src/mongo/shell/collection.js:522:1 DBCollection.prototype.update@src/mongo/shell/collection.js:552:18 @(shell):1:1
No array filter found for identifier 'f' in path 'FieldsCollection.$[].Fields.$[f].Fields'
Are you including the arrayFilters attribute in your query ? This works for me in mongo shell 3.6.
Could it be because I'm using RoboMongo?
@LiranFriedman Yes, it doesn't work in robomongo. It works in the mongo shell, but it doesn't give you expected result. It removes Fields with all nested properties. It doesn't preserve "item1": "value1" and "item2": "value2".
|
1

As your Fields.Fields is a object it is quite difficult.

This would be the query for removing :

db.getCollection('Devices').update(
    {Name : "test", "FieldsCollection.Fields.FieldName":"ABC"},
    {$unset: {"FieldsCollection.Fields.$.Fields":1}},
    {multi:true}
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.