$ filter在mongodb中最多2个嵌套级别 [英] $filter upto 2 nested level in mongodb

查看:64
本文介绍了$ filter在mongodb中最多2个嵌套级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据库:

 {
  "Hospitais": [
    {
      "utis": [
        {
          "_id": 893910,
          "nome": "UTI1",
          "leitos": [
            {
              "_id": 128938120,
              "_paciente": "Oliver"
            },
            {
              "_id": 12803918239,
              "_paciente": "Priscilla"
            }
          ]
        },
        {
          "_id": 38471839,
          "nome": "UTI2",
          "leitos": [
            {
              "_id": 48102938109,
              "_paciente": "Serveró"
            },
            {
              "_id": 501293890,
              "_paciente": "Thales"
            }
          ]
        },
        {
          "_id": 58109238190,
          "nome": "UTI3",
          "leitos": [
            {
              "_id": 93801293890,
              "_paciente": "Lucia"
            },
            {
              "_id": 571029390,
              "_paciente": "Amanda"
            }
          ]
        }
      ],
      "nome": "Dorio Silva"
    },
    {
      "utis": [
        {
          "_id": 410923810,
          "nome": "UTI1",
          "leitos": [
            {
              "_id": 48102938190,
              "_paciente": "Neymar"
            },
            {
              "_id": 48102938190,
              "_paciente": "Thiago"
            }
          ]
        },
        {
          "_id": 41092381029,
          "nome": "UTI2",
          "leitos": [
            {
              "_id": 10293182309,
              "_paciente": "Brazza"
            },
            {
              "_id": 38190238,
              "_paciente": "Pelé"
            }
          ]
        },
        {
          "_id": 83102938109,
          "nome": "UTI3",
          "leitos": [
            {
              "_id": 810923810,
              "_paciente": "Aparecida"
            },
            {
              "_id": 20938904209,
              "_paciente": "Pimentinha"
            }
          ]
        }
      ],
      "nome": "Apart Hospital"
    }
]
} 

我只需要返回与输入"Hospitais.nome"匹配的医院数组,并且在医院数组的每个数组中都有一个名为"utis"的数组,我也想将其过滤在一起, 以下是预期结果" .

我已经通过许多不同的方式尝试过此方法,而我最近尝试过的方法是此代码:

db.collection.aggregate(
[
    { "$match": { "Hospitais.nome": 'Dorio Silva'} },
    {
        "$project": {
            _id: 0,
            Hospitais: {
                $filter: {
                    input: "$Hospitais",
                    as: "hospital",
                    cond: { $and: [{$eq: ["$$hospital.nome", 'Dorio Silva']},{ $eq: ["$$hospital.utis.nome",'UTI1']}]}
                }
            }
        }
    }
]

);

据我对聚合过滤器的了解,它只应显示Hospital.nome为"Dorio Silva"的Hospitais数组的对象和Hospital.utis.nome为"UTI1"的Hospital.utis的对象

我期望的是:

 [
	{
		"utis" : [
			{
				"_id" : NumberInt("893910"),
				"nome" : "UTI1",
				"leitos" : [
					{
						"_id" : NumberInt("128938120"),
						"_paciente" : "Oliver"
					},
					{
						"_id" : NumberLong("12803918239"),
						"_paciente" : "Priscilla"
					}
				]
			}
		],
		"nome" : "Dorio Silva"
	}
] 

但是那绝不是结果,我可以按要求发布结果,但我认为可能不需要. 查询结果的正确方法是什么?我建立数据库的方式有什么问题吗?可以做得更好吗?

解决方案

您需要 $filter 在嵌套数组上

db.collection.aggregate([
  { "$unwind": "$Hospitais" },
  { "$match": { "Hospitais.nome": "Dorio Silva" } },
  { "$project": {
    "Hospitais": {
      "$filter": {
        "input": "$Hospitais.utis",
        "as": "uti",
        "cond": {
          "$eq": ["$$uti.nome", "UTI1"]
        }
      }
    }
  }}
])

或者您也可以尝试

db.collection.aggregate([
  { "$match": { "Hospitais.nome": "Dorio Silva" } },
  { "$project": {
    "Hospitais": {
      "$filter": {
        "input": {
          "$map": {
            "input": "$Hospitais",
            "as": "hospital",
            "in": {
              "nome": "$$hospital.nome",
              "utis": {
                "$filter": {
                  "input": "$$hospital.utis",
                  "as": "uti",
                  "cond": {
                    "$eq": ["$$uti.nome", "UTI1"]
                  }
                }
              }
            }
          }
        },
        "as": "hospital",
        "cond": {
          "$eq": ["$$hospital.nome", "Dorio Silva"]
        }
      }
    }
  }}
])

两者都会给出相似的输出

[
  {
    "Hospitais": [
      {
        "_id": 893910,
        "leitos": [
          {
            "_id": 1.2893812e+08,
            "_paciente": "Oliver"
          },
          {
            "_id": 1.2803918239e+10,
            "_paciente": "Priscilla"
          }
        ],
        "nome": "UTI1"
      }
    ]
  }
]

i have a database structured like this:

{
  "Hospitais": [
    {
      "utis": [
        {
          "_id": 893910,
          "nome": "UTI1",
          "leitos": [
            {
              "_id": 128938120,
              "_paciente": "Oliver"
            },
            {
              "_id": 12803918239,
              "_paciente": "Priscilla"
            }
          ]
        },
        {
          "_id": 38471839,
          "nome": "UTI2",
          "leitos": [
            {
              "_id": 48102938109,
              "_paciente": "Serveró"
            },
            {
              "_id": 501293890,
              "_paciente": "Thales"
            }
          ]
        },
        {
          "_id": 58109238190,
          "nome": "UTI3",
          "leitos": [
            {
              "_id": 93801293890,
              "_paciente": "Lucia"
            },
            {
              "_id": 571029390,
              "_paciente": "Amanda"
            }
          ]
        }
      ],
      "nome": "Dorio Silva"
    },
    {
      "utis": [
        {
          "_id": 410923810,
          "nome": "UTI1",
          "leitos": [
            {
              "_id": 48102938190,
              "_paciente": "Neymar"
            },
            {
              "_id": 48102938190,
              "_paciente": "Thiago"
            }
          ]
        },
        {
          "_id": 41092381029,
          "nome": "UTI2",
          "leitos": [
            {
              "_id": 10293182309,
              "_paciente": "Brazza"
            },
            {
              "_id": 38190238,
              "_paciente": "Pelé"
            }
          ]
        },
        {
          "_id": 83102938109,
          "nome": "UTI3",
          "leitos": [
            {
              "_id": 810923810,
              "_paciente": "Aparecida"
            },
            {
              "_id": 20938904209,
              "_paciente": "Pimentinha"
            }
          ]
        }
      ],
      "nome": "Apart Hospital"
    }
]
}

I need to return just the array of Hospitais that have a match with a input of 'Hospitais.nome' and also, there is a array inside each array of Hospitais called 'utis', i want to filter it too together, there is a "Expected Result" bellow.

I have tried this from many different ways and the most recent one i've been trying is this code :

db.collection.aggregate(
[
    { "$match": { "Hospitais.nome": 'Dorio Silva'} },
    {
        "$project": {
            _id: 0,
            Hospitais: {
                $filter: {
                    input: "$Hospitais",
                    as: "hospital",
                    cond: { $and: [{$eq: ["$$hospital.nome", 'Dorio Silva']},{ $eq: ["$$hospital.utis.nome",'UTI1']}]}
                }
            }
        }
    }
]

);

As far as i understood about aggregation filter, it should only show the objects of the array Hospitais where Hospital.nome is 'Dorio Silva' and the object of Hospital.utis where Hospital.utis.nome is 'UTI1'

What i was expecting:

[
	{
		"utis" : [
			{
				"_id" : NumberInt("893910"),
				"nome" : "UTI1",
				"leitos" : [
					{
						"_id" : NumberInt("128938120"),
						"_paciente" : "Oliver"
					},
					{
						"_id" : NumberLong("12803918239"),
						"_paciente" : "Priscilla"
					}
				]
			}
		],
		"nome" : "Dorio Silva"
	}
]

But thats never the result, i can post the results if asked, but i don't think it may be needed. What is the right way to query my result? Is there anything wrong with the way i'm building my database? It could be done better?

解决方案

You need to $unwind the first array then you can easily apply $filter on the nested array

db.collection.aggregate([
  { "$unwind": "$Hospitais" },
  { "$match": { "Hospitais.nome": "Dorio Silva" } },
  { "$project": {
    "Hospitais": {
      "$filter": {
        "input": "$Hospitais.utis",
        "as": "uti",
        "cond": {
          "$eq": ["$$uti.nome", "UTI1"]
        }
      }
    }
  }}
])

Or you can try this as well

db.collection.aggregate([
  { "$match": { "Hospitais.nome": "Dorio Silva" } },
  { "$project": {
    "Hospitais": {
      "$filter": {
        "input": {
          "$map": {
            "input": "$Hospitais",
            "as": "hospital",
            "in": {
              "nome": "$$hospital.nome",
              "utis": {
                "$filter": {
                  "input": "$$hospital.utis",
                  "as": "uti",
                  "cond": {
                    "$eq": ["$$uti.nome", "UTI1"]
                  }
                }
              }
            }
          }
        },
        "as": "hospital",
        "cond": {
          "$eq": ["$$hospital.nome", "Dorio Silva"]
        }
      }
    }
  }}
])

Both will give the similar output

[
  {
    "Hospitais": [
      {
        "_id": 893910,
        "leitos": [
          {
            "_id": 1.2893812e+08,
            "_paciente": "Oliver"
          },
          {
            "_id": 1.2803918239e+10,
            "_paciente": "Priscilla"
          }
        ],
        "nome": "UTI1"
      }
    ]
  }
]

这篇关于$ filter在mongodb中最多2个嵌套级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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