mongodb删除空数组的数组条目 - 不能使用带ObjectId的$ exists

时间:2016-06-19 08:56:06

标签: mongodb mongoose mongodb-query mongoose-schema

我试图让以下查询工作,我在这篇文章中找到了:

Remove array entries containing an empty array

然而,我收到错误

错误:对于ObjectId,不能使用$。

我不确定它是否是版本的东西,我使用的是Mongo DB版本3.2.6

由于

 var ProductRateSchema = new Schema({
        product: {
            type: Schema.ObjectId,
            ref: 'products'
        },
        user: {
            type: Schema.ObjectId,
            ref: 'User'
        },
        rates: [{
            type: Schema.ObjectId,
            ref: 'rates'
        }]
    });

    var InventorySchema = new Schema({
        name: {
            type: String,
            default: '',
            required: 'Please enter in a name',
            trim: true
        },
        created: {
            type: Date,
            default: Date.now
        },
        user: {
            type: Schema.ObjectId,
            ref: 'User'
        },
        products: [productRateSchema]

    });


    var inventoryId = req.body.inventoryId;
    var productId = req.body.productId;


    Inventory.update(
        { "products.rates.0": { "$exists": false } },
        { 
            "$pull": {
                "products": { "rates.0": { "$exists": false } }
            }
        },
        { "multi": true },
        function(err,numAffected) {

        }
    )

0 个答案:

没有答案