Enrique GF
Enrique GF

Reputation: 1295

update a nested array of strings inside an array of objects in a schema in mongo

I´m trying to update my database schema in its user topic . Its schema would look like this:

name: [Object],
      nickname: [Object],
      email: [Object],
      password: [Object],
      image: [Object],
      googleFlag: [Object],
      groupOfChats: [Array],
      role: [Object],
      messages: [Array],
      userState: [Object],
      friends: [Array]

Where the item to modify would be the groupOfChats that is an array that contains several objects and on the objects there is an item 'memberId' which is a array of string ,being this last one the one i want to access to modify:

groupOfChats: [
    {
      idGroup: { type: String, required: true },
      nameGroup: { type: String, required: true },
      membersId: [{ type: String, required: false }],
      groupCreatorId: { type: String, required: true },
      messages: [{ type: String, required: false }],
      groupImage: { type: String, required: false },
    },
  ],

Traying to access that membersId item in a specific group i just tried to set this:

           let friendsAddedIdOnly =["des","pa","cito"];
            let userChatGroupUpdate = User.updateOne(
                {
                  _id: payload.idUserCreatorGroup,
                  "groupOfChats.idGroup": payload.groupId,
                },

                { $push: { "membersId.$[]": friendsAddedIdOnly} },
                { new: true }

              );

              (await userChatGroupUpdate).save();

a view of my mongo database would be like this: enter image description here

Upvotes: 1

Views: 179

Answers (2)

Enrique GF
Enrique GF

Reputation: 1295

Gues this is the right approach. By the way thanks to @Murat Colyaran for being so helpful:

User.updateOne(
                {
                  _id: payload.idUserCreatorGroup,
                  "groupOfChats.idGroup": payload.groupId,
                },
                {
                  $push: {
                    "groupOfChats.$.membersId": { $each: friendsAddedIdOnly },
                  },
                }
              );
              (await userChatGroupUpdate).save();

Upvotes: 1

Murat Colyaran
Murat Colyaran

Reputation: 2189

Edit:

Old asnwer wasn't working you're right. But you can use below aggregation

db.collection.aggregate([
  {
    "$match": {
      _id: payload.idUserCreatorGroup
    }
  },
  {
    "$set": {
      "groupOfChats.0.membersId": {
        "$reduce": {
          "input": "$groupOfChats.membersId",
          "initialValue": friendsAddedIdOnly,
          "in": {
            "$concatArrays": [
              "$$this",
              "$$value"
            ]
          }
        }
      }
    }
  },
  {
    "$set": {
      "groupOfChats.0": {
        $concatArrays: [
          {
            $slice: [
              "$groupOfChats.0",
              1
            ]
          },
          {
            $slice: [
              "$groupOfChats.0",
              {
                $add: [
                  1,
                  1
                ]
              },
              {
                $size: "$groupOfChats.0"
              }
            ]
          }
        ]
      }
    }
  }
])

Playground

Upvotes: 2

Related Questions