Mongoose查询值不在Array Schema中

时间:2016-10-22 10:32:47

标签: node.js mongodb mongoose

我有一个猫鼬模式如下。

const CampaignSchema = new mongoose.Schema({
        name: {
            type: String,
            unique: true,
            required: true
        },

        enabled:{
            type: Boolean,
            required:true
        },
        expiring:{
            type: Date,
            required:true
        },
        likes: {
            type: Array,
            default: []
        },
        image:{
            type: String
        }
    },
{
    timestamps: true // Saves createdAt and updatedAt as dates. createdAt will be our timestamp.
});

我正在尝试运行一个返回集合的查询,其中字符串值不是like数组的项目。

我该怎么做?我尝试过以下查询:

查询1

var email = [];
        email[0] = req.body.email;
            Campaign.find({"likes" : {"$nin": email}}, function (err, result) {
                if (err) {
                    console.log(err);
                    return res.status(400).json({
                        success: false,
                        message: 'An error occurred on trying to pull the campaigns ' + err
                    });
                }

                res.status(200).json({success: true, result: result, message: 'Successfully pulled the Campaigns '});
            }).limit(20);

查询2

Campaign.find({"likes" : { "$not": { "$all": [req.body.email] } }}, function (err, result) {
                if (err) {
                    console.log(err);
                    return res.status(400).json({
                        success: false,
                        message: 'An error occurred on trying to pull the campaigns ' + err
                    });
                }

                res.status(200).json({success: true, result: result, message: 'Successfully pulled the Campaigns '});
            }).limit(20);

以上都没有奏效。

0 个答案:

没有答案