0

I have an array like this:

{
    "_id" : ObjectId("5ae5afc93e1d0d2965a4f2d7"),
    "entries" : [ 
        {
            "_id" : ObjectId("5b159ebb0ed51064925dff24"),
            "tags" : [ 
                ObjectId("5b142608e419614016b89925"), 
                ObjectId("5b142b40e419614016b89937")
            ]
        }
    ],
    "tags" : [ 
        {
            "_id" : ObjectId("5b142608e419614016b89925"),
            "name" : "Outdated",
            "color" : "#3498db"
        }, 
        {
            "_id" : ObjectId("5b142b40e419614016b89937"),
            "name" : "Todo",
            "color" : "#e8b00a"
        }
    ],
}

I would like to remove "tagId2" from every item in entries. I'm trying to use the position operator for this which is supposed to work with arrays:

db.projects.updateOne(
{
  "_id" : ObjectId("5ae5afc93e1d0d2965a4f2d7"),
},
{
  $pull: {
    'entries.$[].tags': ObjectId("5b142b40e419614016b89937")
  }
})

But I get a MongoDB error:

WriteError: cannot use the part (entries of entries.$[].tags) to traverse the element ([the entries array])

How do I use it correctly? Thanks.

Dominic
  • 62,658
  • 20
  • 139
  • 163
  • By using MongoDB 3.6. The error message says that's not the version you have. Check `db.version()`. – Neil Lunn Jun 09 '18 at 21:28
  • Actually if I run db.version() I get `3.6.5` – Dominic Jun 09 '18 at 21:32
  • Check also that you are not using something like Robo 3T instead of the mongo shell. I made a specific note on the answer for the duplicate question about that, even though I believe that applies mostly to `arrayFilters` but there might have been part of the shell code that I missed which is checking that. Show a full document without obfuscating the `ObjectId` value as an example. But for what I believe I have as an example of your documents already the statement works fine on supported versions. – Neil Lunn Jun 09 '18 at 21:35
  • Thanks, hm how strange! I was using Robo 3T but get the same message via Node. Will add full example – Dominic Jun 09 '18 at 21:44
  • @NeilLunn Updated – Dominic Jun 09 '18 at 21:47
  • If you're using Mongoose then make sure you have a 5.x version. But as stated already test with the `mongo` shell. We know that works and we know it works as long as the core driver is also reported by MongoDB 3.6. As for the updated document, well I already had that data, remember? So I already know the update works on supported versions. – Neil Lunn Jun 09 '18 at 21:50
  • I'm using the latest mongoose too, even directly in the shell it says the same thing: https://ibb.co/iMQhF8 – Dominic Jun 09 '18 at 22:01
  • Took me a moment to spin up an "exact" MongoDB 3.6.5 instance. Copy and and paste document and update statement and it all works as expected. The only explanations I have are that you either 1. Are reading shell version and not `db.version()` 2. Possibly running on an instance upgraded from previous versions and have not enabled new features yet? 3. This is not actually MongoDB and possibly CosmosDB?. The third one would indeed make me unhappy if it is indeed something else masquerading as a latest release MongoDB. But on "supported environment" this does not reproduce. – Neil Lunn Jun 09 '18 at 22:14
  • See also [setFeatureCompatibilityVersion](https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/) in the documentation just in case. – Neil Lunn Jun 09 '18 at 22:15
  • Thanks for taking the time to test it, you were right, I had started this db on 3.(4?) and that was the issue. I didn't realise the upgrade path would require some work there but since it's all test data I deleted it all and started again and now it runs. Thanks again I wouldn't have tried that for quite some time of struggling – Dominic Jun 09 '18 at 22:31
  • That's okay. I added the note to the existing answer. It does not happen in "every" case, but just in those where you needed to upgrade some other component existing in the data. – Neil Lunn Jun 09 '18 at 22:36

0 Answers0