Mongoose Find - 排除一个特定文档

flo*_*olu 9 mongoose mongodb node.js

我想通过 获取许多文档Schema.find(),但通过其 id排除一个特定文档。目前,我的查询如下所示:

Product
    .find({
        $or: [
            { 'tags': { $regex: criteria, $options: 'i' }, },
            { 'name': { $regex: criteria, $options: 'i' }, },
        ],
    })
    .limit(10)
    .exec((err, similar) => {
        //...
    })
Run Code Online (Sandbox Code Playgroud)

我试图添加$not: { _id: someId }到查询中,但这给了我一个错误,这是$not无效的。

Ron*_*537 17

使用$ne代表not equal

Product.find({ _id: {$ne: someId}})
Run Code Online (Sandbox Code Playgroud)

所以整个查询看起来像

Product
    .find({
        $and: [
             { _id: {$ne: someId} },
             { $or: [
                   { 'tags': { $regex: criteria, $options: 'i' }, },
                   { 'name': { $regex: criteria, $options: 'i' }, },
             ]},
         ]
    })
    .limit(10)
    .exec((err, similar) => {
        //...
    })
Run Code Online (Sandbox Code Playgroud)