1

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

11 Comments

Hello Murat.This answer was quite elaborated , but still doesn´t work. Thanks for your support!!!
Sorry to hear it @EnriqueGF can you show some records from your database?
that the point there is not update in database
@EnriqueGF I couldn't understand. No data in the database? I think you misunderstood my question. I asked you to show an example of the data from database.
Ok take a look?
|
1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.