简体   繁体   English

更新或推入数组猫鼬

[英]Update or push in an array mongoose

Sorry for my bad english i'm french and I do my best for speak english ! 对不起,我的英语不好,我是法语,我会尽力说英语! This is my issue: I have a model User . 这是我的问题:我有一个模型User

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
});

I want to make links between users in my nodejs app. 我想在我的nodejs应用程序中的用户之间建立链接。 For this I want to search for a link : 为此,我想搜索一个链接:

if it exist: update the value of this link 如果存在:更新此链接的值

if not: create a new link 如果没有:创建一个新链接

My solution 我的解决方案

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!');
        }
    );

My solution give me this error : 我的解决方案给我这个错误

    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)

How to do what I want to do ? 我该怎么办? Thank's in advance for anwers ! 预先感谢您的答复!

It's fixed: @dnickless gave me the solution I need: Swap to this code : 它是固定的:@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');
        }
    }
);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM