3

I am trying to get the below query to work which I found in this post:

Remove array entries containing an empty array

However, I get the error

Error: Can't use $exists with ObjectId.

I am not sure if it is a version thing, I am using Mongo DB version 3.2.6

Thanks

 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) {

        }
    )
Community
  • 1
  • 1
Onix
  • 31
  • 4

0 Answers0