Home > Enterprise >  How to update all the data that have same field without modify other data in mongoose?
How to update all the data that have same field without modify other data in mongoose?

Time:10-20

I have this message collection compose of 4 fields _id, conversationId, message, seen. In my message controller every time the user click specific user the backend will send a list of messages that have same conversationId into the frontend(reactJS). In the frontend that list of messages will be modify by changing the value of seen from false to true. Then I'm planning to pass this to the backend. My problem is how can I modify only all the data that have same conversationId without replacing all of the data inside message collection

Controller that will get all the messages that have same conversationID

export const getMessage = async (req, res) => {
  try {
    const message = await messageModel
      .find({
        conversationId: req.params.messageId,
      })
      .populate('senderId');

    res.status(200).json(message);
  } catch (error) {
    res.status(500).json({ msg: error.message });
  }
};

Return Value

[
  {
    _id: '616d76e858abdc3fa4059ee3',
    conversationId: '61696e3ed94cd23f8c22f75a',
    message: 'Sample One',
    seen: false
  },
  {

    _id: '616d779458abdc3fa4059f53',
    conversationId: '61696e3ed94cd23f8c22f75a',
    message: 'Sample Two',
    seen: false
  }
]

Frontend function that will change the value of seen

const handleUpdateSeen= (conversation) => {
    dispatch(updateTaskSeenById(conversation));
};

Value that will sending to backend and the output that I want to be change on messageCollection

[
  {
    _id: '616d76e858abdc3fa4059ee3',
    conversationId: '61696e3ed94cd23f8c22f75a',
    message: 'Sample One',
    seen: true
  },
  {

    _id: '616d779458abdc3fa4059f53',
    conversationId: '61696e3ed94cd23f8c22f75a',
    message: 'Sample Two',
    seen: true
  }
]

Solution I made

export const updateMessageSeen = async (req, res) => {
  try {
    var updatedData;

    for (let i = 0; i < req.body.length; i  ) {
      update = {
        _id: req.body[i]._id,
        conversationId: req.body[i].conversationId,
        senderId: req.body[i].senderId._id,
        messageText: req.body[i].messageText,
        messageMedia: req.body[i].messageMedia,
        seen: req.body[i].seen,
        createdAt: req.body[i].createdAt,
      };
    }
    await messageModel.updateMany(
      { conversationId: req.params.conversationId },
      updatedData
    );
  } catch (error) {
    res.status(500).json({ msg: error.message });
  }
};

CodePudding user response:

You may use Model.updateMany to update multiple documents in a collection.

export const seenMessageById = async (req, res) => {
    try {
        if (Array.isArray(req.body) && req.body.length > 0) {
            const idList = req.body.map( message => message._id);
            const result = await messageModel.updateMany({ _id: { $in: idList }}, { seen: true });
            res.status(200).json({ msg: `Total ${result.nModified} documents updated` });
        } else {
            res.status(400).json({ msg: 'Atleast 1 message required to update.' });
        }
    } catch (error) {
        res.status(500).json({ msg: error.message });
    }
};
  • Related