How to find a document where either one or another field matches a value?

前端 未结 1 1243
予麋鹿
予麋鹿 2021-01-17 00:43

My model was,

var userSchema = new Schema({
    first_name : String,
    last_name : String,
    ...
});

From this, I need to do search fil

1条回答
  •  清酒与你
    2021-01-17 01:27

    User.findOne({
        $or: [
            {first_name: name},
            {last_name: name},
        ],
    }, function(err, user) {
        ...
    })
    

    Use find if you expect there to be more than one document matching name to be equal to first_name or last_name.

    0 讨论(0)
提交回复
热议问题