如何在mongodb中获取特定文档之前的所有文档?

时间:2017-06-09 05:40:11

标签: node.js mongodb

我正在实施聊天模块。我在每个对话中都有消息,当特定用户打开对话时,我想要编辑其他人发送的所有消息。

消息文档如下所示:

{ 
  "_id" : ObjectId("593a2a8fd957d847ba45eb6b"), 
  "senderId" : ObjectId("590c53a85fba594a59fe3d0f"), 
  "clientMessageId" : "02598F68-39BD-407A-ADDF-717148CB071C", 
  "conversationId" : ObjectId("592e7fab801be524e0df2591"), 
  "messageType" : "text", 
  "message" : "Yo", 
  "created" : NumberInt(1496984208), 
  "isRead" : false, 
  "isDelivered" : false, 
  "isDeleted" : false, 
  "isEdited" : false
}

mongo代码如下:

let filter = {
    conversationId: ObjectId(conversationId), //To get the conversation
    senderId: {
        $ne: ObjectId(userId) //All the messages sent by others 
    },
    isRead: false, //unread
    _id: {
        $lte: ObjectId(lastMessageId) //All the messages before the given messageid
    }
};

let update = {
    $set: {
        isRead: true,
        readTime: createdTime
    }
};

connection.collection("messagesv2").updateMany(filter, update, (err, result) => {
    if (err) {
        console.log(err);
    }
    console.log(result)

这不起作用。任何人都可以指出为什么或如何实现相同的目标?

0 个答案:

没有答案