3

I currently have a problem where I have to update entries in a deeply nested Document. Now to simplify my problem I have this example. Let's assume I store cars in my MongoDB. A Document would look like this

{
  Make: "BMW",
  Model: "3Series",
  Wheels: [
    {
      _id: someObjectId
      Size: "19 inch",
      Screws: [
        {
          _id: someObjectId
          Type : "M15x40"
        },
        {
          _id: someObjectId
          Type : "M15x40"
        }
      ]
    }
  ]
}

Now if I want to update a specific Wheel, my code would look somewhat like this

CarModel.findOneAndUpdate({
  "_id": CarId, "Wheels._id": WheelId
}, {
  "$set" : {
    "Wheels.$.Size": NewSize
  }
})

Now this works. But I am pretty lost on how I would update an specific screw as I am going through 2 Arrays. Any Idea how I could make this work?

relief.melone
  • 3,042
  • 1
  • 28
  • 57

1 Answers1

2

You need arrayFilters functionality to define the path for more than one nested array:

CarModel.findOneAndUpdate(
    { "_id": CarId },
    { $set: { "Wheels.$[wheel].Screws.$[screw].Type": "something" } },
    { arrayFilters: [ { 'wheel._id': WheelId }, { 'screw._id': screwId } ] })
mickl
  • 48,568
  • 9
  • 60
  • 89
  • nice. That looks cleaner also for the already working use case. I'll try it out immediately. Thanks ;) Is it possible that the mongoose documentation is pretty incomplete on that topic or am I just unable to find this? – relief.melone Apr 30 '19 at 08:56
  • Ok. I will have to figure out how to use it with TypeScript as it is missing in their types as by the looks of it. Will come back to this ;) – relief.melone Apr 30 '19 at 09:10
  • @relief.melone not sure if you can find it in mongoose documentation, it's just MongoDB syntax and mongoose as ODM will send it directly to the database – mickl Apr 30 '19 at 09:23
  • hmmm. I keep getting cannot use the part (Wheels Wheels.$[wheel].Screws... to traverse the element. Aslo weird that the @types/mongoose don't include arrayFilters in their QueryFindOneAndUpdateOptions interface – relief.melone Apr 30 '19 at 11:09
  • You have double `Wheels` in your path, could you fix that and check again ? The thing is that both _id should match, otherwise you will be getting this error – mickl Apr 30 '19 at 11:28
  • I also went down to just Wheels.$[wheel].Size but i get the same error :/. At least I found out that with updateOne instead of findOneAndUpdate I dont get a typing error – relief.melone Apr 30 '19 at 11:30
  • Try to check if wheelId and screwId are exactly the same values (and types) as in your database, the path in my answer looks fine – mickl Apr 30 '19 at 11:45
  • yes. it also matches the official documentation of mongodb, so I'm not sure if this is some issue with the types or mongoose. I have accepted this answer and I will come back with another comment as soon as I figured out what the problem in my case was – relief.melone Apr 30 '19 at 12:49
  • ok. I spent hours, but still was not able to make it work with mongoose that way. I also started to check the mongoose module and it might be a but with :/ I'll update this thread as soon as I know more. Have to remove the accepted answer temporarily though :/ – relief.melone Apr 30 '19 at 17:39
  • @relief.melone here's how I'm testing it: https://pastebin.com/vtQXhvDk , you can try on your own db – mickl Apr 30 '19 at 17:44
  • but thats native mongodb drivers not mongoose no? It looks like the problem is really mongoose here :/ – relief.melone Apr 30 '19 at 18:17
  • That's true, it seems like `arrayFilters` are only supported for updateOne and updateMany, could you test that using one of those methods ? https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate – mickl Apr 30 '19 at 18:21
  • So could you paste your code somewhere and I'll try to reproduce it using mongoose – mickl May 02 '19 at 09:04
  • I can do that. But let me check something out first. Mongoose acutally also exposes the native MongoDriver under "NameOfModel".collection... but that threw the same error and I just found out that even being told connecting to MongoDb V3.6 it's actually a 3.2 and I couldn't find arrayFilters in the v3.2 documentation :/ – relief.melone May 02 '19 at 10:46
  • @relief.melone version might also be the case, unfortunately there's no other way than this for more than one level of nested arrays – mickl May 02 '19 at 11:12
  • 1
    Alright. So I updated the DB to V4.0 and see there. It works as a charm. So I marked the answer as accepted again and for anyone who Is having the same issues. Make sure you have at least MongoDB V3.6. Thanks again – relief.melone May 03 '19 at 08:36