0

assume i have an document

{ _id: 5e53c14c561efe357053a818,
  convoId: 5e53c14c561efe357053a816,
  message:
   [ { isRead: true,
       createdAt: 2020-02-24T12:27:56.085Z,
       _id: 5e53c14c561efe357053a819,
       senderId: 5e4e50a5ae51b02f08f174eb,
       recieverId: 5e4ae76bd4e24418a020b665,
       body: 'hiii' },
     { isRead: true,
       createdAt: 2020-02-24T12:29:12.333Z,
       _id: 5e53c198561efe357053a81c,
       senderId: 5e4e50a5ae51b02f08f174eb,
       recieverId: 5e4ae76bd4e24418a020b665,
       body: 'jawab de' },
     { isRead: true,
       createdAt: 2020-02-24T15:17:12.954Z,
       _id: 5e53e8f8d22dd714ac7f1ee1,
       senderId: 5e4e50a5ae51b02f08f174eb,
       recieverId: 5e4ae76bd4e24418a020b665,
       body: 'hi again' },
     { isRead: true,
       createdAt: 2020-02-25T21:07:15.695Z,
       _id: 5e558c8367d9d61e2c52d0db,
       senderId: 5e4ae76bd4e24418a020b665,
       recieverId: 5e4e50a5ae51b02f08f174eb,
       body: 'whats up??' },
     { isRead: true,
       createdAt: 2020-02-25T21:11:20.653Z,
       _id: 5e558d7867d9d61e2c52d0dc,
       senderId: 5e4ae76bd4e24418a020b665,
       recieverId: 5e4e50a5ae51b02f08f174eb,
       body: 'yoyoy' },
     { isRead: false,
       createdAt: 2020-02-25T21:12:30.743Z,
       _id: 5e558dbe67d9d61e2c52d0dd,
       senderId: 5e4e50a5ae51b02f08f174eb,
       recieverId: 5e4ae76bd4e24418a020b665,
       body: 'nothing much' } ],
  __v: 0 }

i want an array in which senderId is equal to 5e4e50a5ae51b02f08f174eb here what i have done so far

const message = await Message.findOne({
    "message.senderId": "5e4e50a5ae51b02f08f174eb",
  });

but am getting whole result basically i want to filter message array with only 5e4e50a5ae51b02f08f174eb this senderId

i can also done with this

const updatedMessage = message.message.filter(msg => {
      return msg.senderId == "5e4e50a5ae51b02f08f174eb"
  })

but i want to know is there any other method??

Himanshu Rahi
  • 255
  • 4
  • 15

0 Answers0