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

        }
    )

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.