MongoDB:使用 $filter(aggregate) 过滤子数组

迭戈·桑托斯

我从 MongoDB 开始,所以,如果我的问题太基本,抱歉。我在 Mongo 中有一个集合,其结构如下:

{
    nome: "Diego", contas: [ 
    {
        banco: "itau",
        tipo: "corrente",
        investimentos: [{
            tipo: "fundo",
            transacoes: [{
                numero: 1,
                valor: 1000,
                data: "24/06/2017"
            },
            {
                numero: 2,
                valor: 1500,
                data: "01/02/2017"
            }]
        },
        {
            tipo: "poupanca",
            transacoes: [{
                numero: 1,
                valor: 600,
                data: "20/06/2017"
            }]
        }]
    },
    {
        banco: "bradesco",
        tipo: "poupanca",
        investimentos: []
    },
    {
        banco: "santander",
        tipo: "juridica",
        investimentos: [{
            tipo: "pic",
            transacoes: [{
                numero: 1,
                valor: 100,
                data: "20/06/2017"
            },
            {
                numero: 2,
                valor: 100,
                data: "20/05/2017"
            },
            {
                numero: 3,
                valor: 100,
                data: "20/05/2017"
            }]
        }]
    }]
}

我要的筛选investimentos的阵列conta,以包含仅在该领域的条目tipo是等于fundo有我的查询:

db.teste.aggregate([
{
    $project: {
        nome: 1,
        "contas.investimentos": {
            $filter: {
                input: "$contas.investimentos",
                as: "investimento",
                cond: { $eq: ["$$investimento.tipo", "fundo"]}
            }
        }
    }
}]);

结果是:

 {
    "_id" : ObjectId("59563cf574f77220ff2166ea"),
    "nome" : "Diego",
    "contas" : [ 
        {
            "investimentos" : []
        }, 
        {
            "investimentos" : []
        }, 
        {
            "investimentos" : []
        }
    ]
}

我无法理解为什么结果null在 field 上investimentos我认为我处理子数组的方式有问题,但我找不到差距在哪里。有谁可以帮助我吗 ?

s7vr

您必须使用$map来映射contas值并到达$filter investimentos.

就像是

db.teste.aggregate([{
    $project: {
        nome: 1,
        contas: {
            $map: {
                input: "$contas",
                as: "contas",
                in: {
                    banco: "$$contas.banco",
                    tipo: "$$contas.tipo",
                    investimentos: {
                        $filter: {
                            input: "$$contas.investimentos",
                            as: "investimento",
                            cond: {
                                $eq: ["$$investimento.tipo", "fundo"]
                            }
                        }
                    }
                }
            }
        }
    }
}]);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

lodash。使用子数组过滤字段值的数组

来自分类Dev

根据子数组信息使用JSONPath过滤数组

来自分类Dev

子数组中的对象过滤mongodb中的查询

来自分类Dev

MongoDB查询可从嵌套数组中过滤子文档

来自分类Dev

使用underscore.js按子数组中的属性过滤

来自分类Dev

在mongodb中过滤数组

来自分类Dev

使用子字符串作为条件过滤子文档数组

来自分类Dev

MongoDB-$ filter中的聚合$ filter /子文档中子文档的过滤数组

来自分类Dev

如何使用jq在json文件中使用对象过滤子对象中的某些数组

来自分类Dev

MongoDB使用聚合框架过滤深度嵌套的数组

来自分类Dev

子Mongodb分组数组

来自分类Dev

使用数组内容过滤数组

来自分类Dev

使用NSPredicate过滤数组

来自分类Dev

使用 PHP 过滤数组

来自分类常见问题

使用MongoDB Java仅检索数组子文档字段的值

来自分类Dev

在mongodb中使用文档的特定键值对检索选定的子数组

来自分类Dev

使用mongodb在数组中查找子文档

来自分类Dev

MongoDB使用$ pull删除数组子文档中的字典

来自分类Dev

使用$ pull在MongoDB中删除一个(子)数组

来自分类Dev

MongoDB:使用索引更新数组中的子文档

来自分类Dev

使用MongoDB和Mongoose更新子数组属性

来自分类Dev

如何使用子文档数组更新MongoDB文档

来自分类Dev

使用Java对mongodb中的子文档数组进行排序

来自分类Dev

MongoDB:使用索引更新数组中的子文档

来自分类Dev

如何使用子文档数组更新MongoDB文档

来自分类Dev

使用RxJava过滤子列表

来自分类Dev

使用xpath子过滤html

来自分类Dev

在文档的子字段中过滤mongodb游标

来自分类Dev

如何过滤mongoDB中的子文档?

Related 相关文章

热门标签

归档