📌  相关文章
📜  如果不存在则推送数组元素 mongoose - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:31.645000             🧑  作者: Mango

代码示例1
var SomeSchema = new Schema({
    // ...
    members: [
        {
            name: String,
            username: String
        }
    ]
});



//You could check for the username in the condition part of the update query:

var conditions = {
    _id: id,
    'members.username': { $ne: 'something' }
};

var update = {
    $addToSet: { members: { name: 'something', username: 'something' } }
}

SomeModel.findOneAndUpdate(conditions, update, function(err, doc) {
    ...
});