更新或推入数组猫鼬

时间:2018-08-15 20:19:58

标签: javascript node.js mongodb express mongoose

对不起,我的英语不好,我是法语,我会尽力说英语! 这是我的问题: 我有一个模型用户

var userSchema = new Schema({
    name: String,
    username: {type: String, required: true, unique: true},
    password: {type: String, required: true},
    links: [{user_id: Schema.ObjectId,value: Number}],
    created_at: Date,
    updated_at: Date
},{
    strict: true
});

我想在我的nodejs应用程序中的用户之间建立链接。为此,我想搜索一个链接:

如果存在:更新此链接的值

如果没有:创建一个新链接

我的解决方案

var link = {
    user_id: req.body.user_id,
    value: req.body.value
};
User.findByIdAndUpdate({_id: req.params.id, 'links.user_id': link.user_id}, {
            $set: {
                'links.$.value': link.value
            }
        },
        {safe: true, upsert: true},
        (err, user) => {
            if (err) throw err;
            if(!user){
                User.findByIdAndUpdate(req.params.id,{
                    $push:{
                        links:link
                    }
                },
                {safe: true, upsert: true},(err,user) => {
                    if (err) throw err;
                    return res.send('relation créé')
                })
            }
            res.send('Relation mise à jour!');
        }
    );

我的解决方案给了我这个错误

    F:\Projets\the_one_panel\node_modules\mongoose\lib\utils.js:461
        throw err;
        ^
MongoError: The positional operator did not find the match needed from the query.
    at F:\Projets\the_one_panel\node_modules\mongodb-core\lib\connection\pool.js:580:63
    at authenticateStragglers (F:\Projets\the_one_panel\node_modules\mongodb-core\lib\connection\pool.js:503:16)
    at Connection.messageHandler (F:\Projets\the_one_panel\node_modules\mongodb-core\lib\connection\pool.js:539:5)
    at emitMessageHandler (F:\Projets\the_one_panel\node_modules\mongodb-core\lib\connection\connection.js:309:10)
    at Socket.<anonymous> (F:\Projets\the_one_panel\node_modules\mongodb-core\lib\connection\connection.js:452:17)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)

我该怎么办?预先感谢您的答复!

1 个答案:

答案 0 :(得分:0)

它是固定的: @dnickless给了我我需要的解决方案: 交换此代码:

var link = {
    user_id: require('mongoose').Types.ObjectId(req.body.user_id),
    value: req.body.value
};
User.findOneAndUpdate({_id: req.params.id, 'links.user_id': link.user_id}, {
        $set: {
            'links.$.value': link.value
        }
    },
    (err, user) => {
        if (err) throw err;
        console.log(user);
        if(!user){
            User.findByIdAndUpdate(req.params.id,{
                $push:{
                    links:link
                }
            },
            {safe: true, upsert: true},(err,user) => {
                if (err) throw err;
                res.send('Relations créé');
            });
        }
        else{
            res.send('Relations mise à jour');
        }
    }
);

删除{safe: true, upsert: true} 并将findByIdAndUpdate替换为findOneAndUpdate