Skip to main content
prejip
December 5, 2023
Question

MongoDB Connected System - Update query isn't working as expected

  • December 5, 2023
  • 0 replies
  • 0 views

Hi team,

Am working on an app which aims to updated external database(Mongo DB) and for this we are using Mongo DB connected system.

Now we have a use case where we must update an existing collection entry with new details, and we are facing problem to update it correctly.

Below is my collection structure.

 

Col A     

Col B

Col C [

{NestColC1A,     NestCol C1B,      NestColC1C},

{NestColC2A,     NestCol C2B,      NestColC2C},

]

Col D

[

{NestColD1A,     NestCol D1B,      NestColD1C1[

{NestNestColD1C1A1,                    NestNestColD1C1B1,                      NestNestColD1C1C1},

{NestNestColD1C1A2,                    NestNestColD1C1B2                       NestNestColD1C1C2},

{NestNestColD1C1A3,                    NestNestColD1C1B3,                      NestNestColD1C1,C3},

},

{NestColD2A,     NestCol D2B,      NestColD2C1[

{NestColD2C2A1,                             NestColD2C2B1,                               NestColD2C2C1},

{NestColD2C2A2,                             NestColD2C2B2                                 NestColD2C2C2},go on

}

]

 

Basically, Col D has vector value in one of its vector element(NestColD2C*)

And now the requirement is that we want to update the existing record(or existing Document in MongoDB’s context) with a new entry for col D’s vector element i.e

We are trying to insert below entry to existing record:
NestColD1C1: [

{NestNestColD1C1A4,                    NestNestColD1C1B4,                      NestNestColD1C1,C4},

So post update my structure should look like:

Col A     

Col B

Col C [

{NestColC1A,     NestCol C1B,      NestColC1C},

{NestColC2A,     NestCol C2B,      NestColC2C},

]

Col D

[

{NestColD1A,     NestCol D1B,      NestColD1C1[

{NestNestColD1C1A1,                    NestNestColD1C1B1,                      NestNestColD1C1C1},

{NestNestColD1C1A2,                    NestNestColD1C1B2                       NestNestColD1C1C2},

{NestNestColD1C1A3,                    NestNestColD1C1B3,                      NestNestColD1C1,C3},

{NestNestColD1C1A4,                   NestNestColD1C1B4,                      NestNestColD1C1,C4}

 

},

{NestColD2A,     NestCol D2B,      NestColD2C1[

{NestColD2C2A1,                             NestColD2C2B1,                               NestColD2C2C1},

{NestColD2C2A2,                             NestColD2C2B2                                 NestColD2C2C2},go on

}

]

 

 

We have tried $addToSet function but the output we got was like this:

Col A     

Col B

Col C [

{NestColC1A,     NestCol C1B,      NestColC1C},

{NestColC2A,     NestCol C2B,      NestColC2C},

]

Col D

[

{NestColD1A,     NestCol D1B,      NestColD1C1[

{NestNestColD1C1A1,                    NestNestColD1C1B1,                      NestNestColD1C1C1},

{NestNestColD1C1A2,                    NestNestColD1C1B2                       NestNestColD1C1C2},

{NestNestColD1C1A3,                    NestNestColD1C1B3,                      NestNestColD1C1,C3},

 

},

{NestColD2A,     NestCol D2B,      NestColD2C1[

{NestColD2C2A1,                             NestColD2C2B1,                               NestColD2C2C1},

{NestColD2C2A2,                             NestColD2C2B2                                 NestColD2C2C2},go on

}

]

 

NestColD1C1[

{NestNestColD1C1A4,                    NestNestColD1C1B4,                      NestNestColD1C1,C4}

]

Which is incorrect structure.

 

The query to wrote in our update section was below:

$addToSet:{

NestColD1C1{

NestNestColD1C1A4: ri!value,

NestNestColD1C1B4: ri!value2…….

}

}

 

We also tried using dot notation like below but no luck
$addToSet:{

NestColD1C1{

NestColD1C1.NestNestColD1C1A4: ri!value,

NestColD1C1NestNestColD1C1B4: ri!value2…….

}

}

 

 

Any help on this matter would be great and much appreciated.

 

 

Thanks