Mongoose和mongoDB - 重复键错误

时间:2015-02-28 11:47:06

标签: node.js mongodb mongoose socket.io

我尝试使用MongoDB和mongoose进行我的小对话应用,其中每个用户都有自己的收藏,但我在保存新对话时遇到错误:

错误:MongoError:insertDocument ::由:: 11000引起E11000重复键错误索引:chat.privatemessages。$ id dup key:{:ObjectId(' 54f1a39ff467833f7e794636&# 39;)}

我的架构:

    var chatPrivateMessagesSchema = mongoose.Schema({
    user: String,
    with:[{
        conversation_with: String,
        messages:[{
            text:String,
            created: {type: Date, default: Date.now}
        }]
    }]
});

var PrivateMessages = mongoose.model('PrivateMessages', chatPrivateMessagesSchema);

保存新会话的方法:

socket.on ('chat', function (from, to, message, callback) {
    PrivateMessages.find ({}, function (err, users) {
        if (err) throw err;
        for (var i = 0; i < users.length; i++) {
            if (users[i].user == from) {
                var newPrivateMsg = new PrivateMessages ({_id: users[i]._id}, {
                    with: [{
                        conversation_with: to, //if conversation with "to" user does not exist create a new entry
                        messages: [{
                            text: message, //append a new message 
                            created: {type: Date, default: Date.now}
                        }]
                    }]
                });
                newPrivateMsg.save (function (err) {
                    if (err) throw err;
                });
            }
        }
    });
});

所以基本上我想要的输出应该是这样的?

{
    "_id": "some id",
    "user": "John",
    "with": [{
        "conversation_with": "Maria",
        "_id": "some id",
        "messages": [{
            "text": "Hellomyfisrtmessage",
            "created": "somedate"
        }, {
            "text": "Secondmessage ",
            "created": "somedate"
        }, {
            "text": "Thirdmessage",
            "created": "somedate"
        }, ]
    }, {
        "conversation_with": "Peter",
        "_id": "some id",
        "messages": [{
                "text": "Hellomyfisrtmessage",
                "created": "somedate"
            }, {
                "text": "Secondmessage ",
                "created": "somedate"
            }, {
                "text": "Third message",
                "created": "some date"
            },

        ]
    }],

}

我真的在努力学习如何插入新条目:(任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

问题是你要将每个PrivateMessage _id设置为用户的_id,不要这样做!

编辑:

socket.on ('chat', function (from, to, message, callback) {
    PrivateMessages.find ({}, function (err, users) {
        if (err) throw err;
        for (var i = 0; i < users.length; i++) {
            if (users[i].user == from) {
                users[i].with.forEach(function(with){
                    if (with.conversation_with === to ){

                        // append new message
                        with.messages.push({
                            text: message,
                            created: { type: Date, default: Date.now }
                        })
                    }
                })

                users[i].save (function (err) {
                    if (err) throw err;
                });

            }
        }
    });
});