删除包含空数组的数组条目 [英] Remove array entries containing an empty array

查看:110
本文介绍了删除包含空数组的数组条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从没有价格的产品阵列中删除所有产品.在下面的查询中,我尝试检查Rates数组的长度,但是 他们似乎都不起作用.感谢您的帮助.

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.

预先感谢

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

 });

推荐答案

不知道您尝试了什么,因为它根本没有包含在您的问题中,但是检查空数组的最好方法是基本上看一下0索引与

Don't know what you tried since it is simply not included in your question, but the best way to check for an empty array is to basically look where the 0 index does not match $exists:

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

    }
)

.update()语句的查询"部分可确保我们甚至仅尝试触摸"products.rates"中具有空数组的文档.这不是必需的,但是它确实避免了在文档中测试以下"update"语句条件的条件,该条件对于任何数组元素都不是true,因此可以使事情更快一些.

The "query" portion of the .update() statement is making sure that we only even attempt to touch documents which have an empty array in "products.rates". That isn't required, but it does avoid testing the following "update" statement condition on documents where that condition is not true for any array element, and thus makes things a bit faster.

实际的更新"部分在 $pull 上适用"products"数组删除"inner" "rates"为空数组的所有项目.因此,无论如何,$pull中的路径"实际上是在"products"内容内部查找,因此它是相对于此而不是整个文档的.

The actual "update" portion applies $pull on the "products" array to remove any of those items where the "inner" "rates" is an empty array. So the "path" within the $pull is actually looking inside the "products" content anyway, so it is relative to that and not to the whole document.

自然地,$pull将删除单个操作中所有匹配的元素.仅在以下情况下才需要 "multi" 您真的想使用该语句更新多个文档

Naturally $pull will remove all elements that match in a single operation. The "multi" is only needed when you really want to update more than one document with the statement

这篇关于删除包含空数组的数组条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆