I am trying to remove all products from the products array where the product has no rates. In my queries below I tried to check the length of the rates array, but none of them seem to work. Any help is appreciated.
Thanks in advance
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;
// none of these queries work
db.inventory.findOneAndUpdate({ '_id': inventoryId,
{$pull: {'products': { product: productId, 'rates': { $eq:[] }}}}, function (err, result) {
});
db.inventory.findOneAndUpdate({ '_id': inventoryId,
{$pull: {'products': { product: productId, 'rates': {$size: {$lt: 1}}}}}, function (err, result) {
});
db.inventory.findOneAndUpdate({ '_id': inventoryId,
{$pull: {'products': { product: productId, 'rates': null }}}, function (err, result) {
});