且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何查询深度嵌套的json文档?

更新时间:2023-01-17 15:00:13

您可以在下面的汇总查询中进行尝试.

You can try below aggregation query.

$filter内部数组和$arrayElemAt来投影匹配的元素.

$filter inner array and $arrayElemAt to project the matched element.

$map可在所有Kamadas中输出所有匹配的Speletajs元素.

$map to output all matching Speletajs elements across all Kamadas.

$arrayElemAt减少为单个匹配项.对于多个匹配项,请删除$arrayElemAt.

$arrayElemAt to reduce to a single match. For multiple matches remove $arrayElemAt.

db.games.aggregate([
  {"$match":{"Spele.Komanda.Speletaji.Speletajs.Nr":16}},
  {"$project":{
    "Speletaji":{
      "$arrayElemAt":[
        {"$map":{
          "input":"$Spele.Komanda",
          "in":{
            "$arrayElemAt":[
              {"$filter":{
                "input":"$$this.Speletaji.Speletajs",
                "cond":{"$eq":["$$this.Nr",16]}

              }},
            0]
          }
        }},
      0]
    }
  }}
])