4

I have a Message schema:

const messageSchema = new Schema({
    receiver:{
        type: mongoose.Schema.Types.ObjectId,
        ref:'User'
    },
    sender:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'User'
    },
    room:{
        type:String
    },
    message:{
        type:String
    }
},{timestamps:true});

As you can see I am holding a reference to sender.I am getting all messages using:

const messages = await Message.find({room}).sort({createdAt:1}).populate('sender',{email:1,_id:0});

and this returns:

[
{
    _id: 60b2725c3165d72d1a627826,
    receiver: 60abb9e1016b214c7563c8f1,
    sender: { email: 'test2@test.com' },
    room: 'test@test.com--with--test2@test.com',
    message: 'dfgfdsgdf',
    createdAt: 2021-05-29T16:57:00.857Z,
    updatedAt: 2021-05-29T16:57:00.857Z,
    __v: 0
  }
]

I want to remove the email key from the response. So the sender field should be like sender:'test2@test.com'.Is there any way to do this?

Jarnojr
  • 543
  • 1
  • 7
  • 18

2 Answers2

1

This is a special type of object and needs to be converted before we can work with it. Try this and see the link at the end

// messages is a special Mongoose object
const messages = [{
    _id: '60b2725c3165d72d1a627826',
    receiver: '60abb9e1016b214c7563c8f1',
    sender: {
      email: 'test2@test.com'
    },
    room: 'test@test.com--with--test2@test.com',
    message: 'dfgfdsgdf',
    createdAt: '2021-05-29T16:57:00.857Z',
    updatedAt: '2021-05-29T16:57:00.857Z',
    __v: 0
  }
]

// convert it to a normal object
let objs = messages.toObject()

// now we can iterate it 
let newobjs = objs.map(obj => {
  obj.sender = obj.sender.email;
  return obj
});


console.log(newobjs)

How do you turn a Mongoose document into a plain object?

Kinglish
  • 23,358
  • 3
  • 22
  • 43
  • Thanks for your response. That's the correct response. But my query returns an array of objects, not an object. So it's my mistake. I have edited my question. How can I combine this logic with my query? – Jarnojr May 29 '21 at 20:18
  • I don't know why but It's still the same. Thank you so much though. – Jarnojr May 29 '21 at 21:35
  • I don't fully get how the mongoose end of things works, but what about this: `Message.find({room}).sort({createdAt:1}).populate('sender',{email:1,_id:0}).map(obj => ({ ...obj, sender: obj.sender.email }))` – Kinglish May 29 '21 at 22:01
  • Ok, I found something that might help. Check out the revised answer – Kinglish May 30 '21 at 05:57
  • 1
    `toObject()` didn't work for me. But from your reference, I have tried the `lean()` function, and it worked. So this is what I have added to my query `lean().exec(function(err,doc){ doc.map(obj =>{ obj.sender= obj.sender.email; return obj;})res.status(200).json(doc)})` . Thank you so much. – Jarnojr May 30 '21 at 14:45
1

you can use this way and rewrite your object

 let obj = {
      _id: '60b2725c3165d72d1a627826',
      receiver: '60abb9e1016b214c7563c8f1',
      sender: { email: 'test2@test.com' },
      room: 'test@test.com--with--test2@test.com',
      message: 'dfgfdsgdf',
      createdAt: '2021-05-29T16:57:00.857Z',
      updatedAt: '2021-05-29T16:57:00.857Z',
      __v: 0
    };
    
    obj = {
      ...obj,
      sender: obj.sender.email,
    };
    console.log('obj',obj)
Favio Figueroa
  • 264
  • 1
  • 8