0

I just have to delete in my object all participants in "roomOption" except me.

I've already seen this : Querying after populate in Mongoose My problem is similar, but in my situation I need to do it inside an array called by the populate

This is my method :

    return MyRoom.findOne(doc, proj).populate({
      path: 'rooms',
      select: ['participants', 'updatedAt', 'roomOption'],
      populate: [
        {
          path: 'participants',
          match: { _id: {$ne:doc.profile}},
          select: ['_meta.pseudo', 'pictureprofile.url']
        },
        {
          path: 'roomOption',
          match: { 'participants.profile':doc.profile },
          // I think I need to specific my participants just here 
        }
      ],
    })

And this is that I receive :

"rooms": [
            {
                "participants": [
                    {
                        "pictureprofile": {
                            "url": "/static/img/user-profile/picture_default_user.png"
                        },
                        "_id": "5d96e48df71c5861c22373bb",
                        "_meta": {
                            "pseudo": "eva_bouvier"
                        }
                    }
                ],
                "_id": "5d9d546d5f72ee21eb400a1f",
                "roomOption": {
                    "_id": "5d9d546d5f72ee21eb400a20",
                    "room": "5d9d546d5f72ee21eb400a1f",
                    "participants": [
                        {
                            "seen": true,
                            "mute": false,
                            "_id": "5d9d546d5f72ee21eb400a23",
                            "profile": "5d96e3f1f71c5861c22373b1",
                            "notification": 0
                        },
                        {
                            "seen": false,
                            "mute": false,
                            "_id": "5d9d546d5f72ee21eb400a22",
                            "profile": "5d96e48df71c5861c22373bb",
                            "notification": 1
                        }
                    ],
                    "createdAt": "2019-10-09T03:30:53.595Z",
                    "updatedAt": "2019-10-09T03:30:53.595Z",
                    "__v": 0
                }
            }
        ],

doc.profile is my profile. Thank you so much if you have the solution. I know I can do it with aggregate but I really want to know if it's possible with findOne

1 Answers1

0

I found the solution, if someone is in the same situation you can do :

    return MyRoom.findOne(doc, proj)
    .sort({'updatedAt':1})
    .populate({
      path: 'rooms',
      select: ['participants', 'updatedAt', 'roomOption'],
      populate: [
        {
          path: 'participants',
          match: { _id: {$ne:doc.profile}},
          select: ['_meta.pseudo', 'pictureprofile.url']
        },
        {
          path: 'roomOption',
          match: { 'participants.profile': doc.profile },
          select: 'participants.$'
        }
      ],
    })