简体   繁体   English

如何在MongoDB中过滤子子文档

[英]How filter sub sub document in MongoDB

Hello I am working with the full Stack 'MEAN' and i have a data structure in MongoDB as indicated below: 您好,我正在使用完整的Stack'MEAN',并且在MongoDB中有一个数据结构,如下所示:

var ExpenseSchema = new Schema({
    date: {
        type: Date,
        default: Date.now,
        required: 'Ingrese la fecha del comprobante'
    },
    supplier: {
        type: Schema.ObjectId,
        ref: 'Supplier',
    },
    created: {
        type: Date,
        default: Date.now
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
});


var SupplierSchema = new Schema({
    name: {
        type: String,
        default: '',
        required: 'Ingrese la Razon Social del Proveedor',
        trim: true
    },
    category: {
        type: Schema.ObjectId,
        ref: 'Category',
    },
    created: {
        type: Date,
        default: Date.now
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
});

var CategorycompraSchema = new Schema({
    name: {
        type: String,
        default: '',
        required: 'Please fill Rubrocompra name',
        trim: true
    },
    created: {
        type: Date,
        default: Date.now
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
});

Each 'Expense' has a 'Supplier' and each supplier has a 'Category' 每个“费用”都有一个“供应商”,每个供应商都有一个“类别”

I need to query so that I filter all 'Expenses' in a particular category. 我需要查询以便过滤特定类别中的所有“费用”。 Someone could tell me how this can be done with MongoDB or mongoose? 有人可以告诉我,如何使用MongoDB或mongoose做到这一点?

That is an important case in mongodb, aggregations in mongodb is the right approach to solve this. 这在mongodb中很重要,mongodb中的聚合是解决此问题的正确方法。 You need to $unwind the supplier array and then category array and then use $group to put it back together: 您需要先展开供应商数组,然后展开类别数组,然后使用$ group将其放回到一起:

My solution may differ depending upon your requirement, but this is something you have to do: 根据您的要求,我的解决方案可能会有所不同,但这是您必须要做的事情:

db.test.aggregate(
    { $match: {...}},   //match query 
    { $unwind: '$supplier'},
    { $unwind: '$supplier.category'},
    { $match: {supplier.category.a: ...}},      //match query after unwinding of supplier and category
    { $group: {_id: '$_id', ...})

It will first unwind the supplier array and then unwind category array 它将首先展开供应商数组,然后展开类别数组

But since you are also using mongoose, you can use plain JavaScript. 但是,由于您还使用猫鼬,因此可以使用纯JavaScript。 You can fetch all expenses and then loop through them and obtain your result 您可以获取所有费用,然后遍历所有费用并获得结果

Expense.find().then(function(expenses) {
    expenses.forEach(function(suppliers){
        suppliers.forEach
        ...
    })
})

Although this javascript way would increase effort in single threaded enviroment(node js), but still it comes in handy for some typical mongodb queries 尽管这种javascript方式会增加单线程环境(node js)的工作量,但对于某些典型的mongodb查询仍然很方便

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM