0

I would like to remove all key/value pairs where the key is url from all documents inside a mongodb collection.

I've been agonising over this all weekend and have only managed to partially solve it using $unset, but I'm struggling to access all items particularly key/values inside objects in arrayKey and also do it in one query/function (if possible).

This is generally how each document looks. There sometimes may be more objects in the arrayKey, but other than that they're pretty similar:

    {
    "keyOne": "String",
    "keyTwo": 7,
    "keyThree": {
        "subKeyOne": "String",
        "url": "String"
    },
    "arrayKey": [
        {
            "arrayKeyOne":"String",
            "url": "String"
        },
        {
            "arrayKeyOne":"String",
            "url": "String"
        }
    ],
    "url":"String"
}

Any help would be greatly appreciated. I'm still new to programming and mongodb so the documentation is a bit dense for a beginner.

Thanks :)

Pete
  • 157
  • 1
  • 10
  • Use `db.coll.update( {}, {$pull: {"arrayKey":{url:{$exists:true}}}}, {multi:true})`. to remove key value pairs. – s7vr Mar 04 '18 at 22:05
  • Possible duplicate of [How to remove array element in mongodb?](https://stackoverflow.com/questions/16959099/how-to-remove-array-element-in-mongodb) – s7vr Mar 04 '18 at 22:09
  • That seems to remove the whole object rather than leaving an array of objects only containing arrayKeyOne. All I'm left with is an empty array. Is this expected behaviour or am I possibly typing it wrong? – Pete Mar 04 '18 at 23:09
  • 1
    Sorry misunderstood your post. Try `db.coll.update( {}, {$unset: {"arrayKey.$[].url:""}}, {multi:true})` in 3.6. More [here](https://stackoverflow.com/questions/19945924/remove-a-field-from-all-elements-in-array-in-mongodb/47836562#47836562) – s7vr Mar 04 '18 at 23:11
  • I just updated to 3.6.3 to make sure, but that query doesn't seem to modify anything – Pete Mar 07 '18 at 21:53
  • Do you get any exception/error ? – s7vr Mar 07 '18 at 21:56
  • No, I just had the amount of documents matched (9) and the amount upserted and modified both 0. I amended the query slightly because I think you missed a quote mark? `db.coll.update( {}, {$unset: {"arrayKey.$[].url":""}}, {multi:true} );` Maybe I did that wrong? – Pete Mar 07 '18 at 22:23
  • Can I see your query pls ? – s7vr Mar 07 '18 at 22:23
  • I posted it above – Pete Mar 07 '18 at 23:02
  • 1
    The query looks good to me and field names all match document in the post. Not sure but did you set the feature compatibility version flag ? More [here](https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/#set-feature-compatibility-version-on-mongodb-3-6-deployments) – s7vr Mar 07 '18 at 23:33
  • Amazing! I didn't even know that existed, it worked. They query worked after that thank you so much :D How would I get to an array inside an object and delete the same url field? I tried `{$unset:{"object.array.$[].url":""}}`No worries if asking two questions in one is taboo on Stack. I'm just trying my luck :P – Pete Mar 08 '18 at 00:06
  • Np. That should work. is it an object or array ? – s7vr Mar 08 '18 at 01:18
  • it's an array of objects inside another object. I want to delete the same url fields. I'm getting error code 2, "The path 'object.array' must exist in the document in order to apply array updates." Some array's don't have the `array` field. – Pete Mar 08 '18 at 12:13
  • 1
    Try `db.coll.update( {"object.array":{$exists:true}}, {$unset: {"object.array.$[].url:""}}, {multi:true})` – s7vr Mar 08 '18 at 12:34
  • I got that from the documentation, with your help it's become a bit easier to understand. Thank you so much :D – Pete Mar 09 '18 at 10:56

0 Answers0