【问题标题】:Mongo Query to get a resultMongo 查询得到结果
【发布时间】:2021-09-25 21:30:54
【问题描述】:

我的 mongo 集合名称测试,其中包含以下文档。

[
  {
    "title": "One",
    "uid": "1",
    "_metadata": {
      "references": [
        {
          "uid": "2"
        },
        {
          "asssetuid": 10
        }
      ]
    }
  },
  {
    "title": "Two",
    "uid": "2",
    "_metadata": {
      "references": [
        {
          "uid": "3"
        },
        {
          "asssetuid": 11
        }
      ]
    }
  },
  {
    "title": "Three",
    "uid": "3",
    "_metadata": {
      "references": []
    }
  }
]

我想要以下格式的结果(对于 uid:1)

[
  {
    "title": "One",
    "uid": 1,
    "_metadata": {
      "references": [
        {
          "asssetuid": 10
        },
        {
          "asssetuid": 11
        },
        {
          "title": "Two",
          "uid": "2",
          "_metadata": {
            "references": [
              {
                "title": "Three",
                "uid": "3"
              }
            ]
          }
        }
      ]
    }
  }
]

对于 uid:2 我想要以下结果

[
  {
    "title": "Two",
    "uid": 2,
    "_metadata": {
      "references": [
        {
          "asssetuid": 11
        },
        {
          "title": "Three",
          "uid": "3"
        }
      ]
    }
  }
]

我在这里使用了哪个查询来获得受人尊敬的结果。根据它的uid。这里我想要父子关系中的结果。这是否可以使用 MongoDB 图形查找查询或我们可以用来获取结果的任何其他查询。请帮我解决这个问题。

新类型输出

[{
"title": "One",
"uid": 1,
"_metadata": {
    "assets": [{
        "asssetuid": 10,
        "parent": 1
    }, {
        "asssetuid": 11,
        "parent": 2
    }],
    "entries": [{
        "title": "Two",
        "uid": "2",
        "parent": 1
    }, {
        "title": "Three",
        "uid": "3",
        "parent": 2
    }]
}

}]

【问题讨论】:

  • 好的...@SurajDalvi 所以你绝对想以嵌套形式解析引用,否则平面地图也可以?

标签: node.js mongodb mongodb-query


【解决方案1】:

Mongo 支持使用 $ref 的自动引用解析,但为此,您需要稍微更改架构并且解析解析仅受某些驱动程序支持。

您需要以这种格式存储您的数据:

[
  ...
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "_metadata": {
      "references": [
        {
          "$ref": "collection",
          "$id": ObjectId("5a934e000102030405000001"),
          "$db": "database"
        },
        {
          "asssetuid": 10
        }
      ]
    },
    "title": "One",
    "uid": "1"
  },
  ....
]

更多关于$ref的信息请参考官方文档:label-document-references

您可以使用$graphLookup 解析引用,但$graphlookup 的唯一问题是您将丢失assetuid。这是查询,它将解析引用并在平面地图中提供输出

db.collection.aggregate([
  {
    $match: {
      uid: "1"
    }
  },
  {
    $graphLookup: {
      from: "collection",
      startWith: "$_metadata.references.uid",
      connectFromField: "_metadata.references.uid",
      connectToField: "uid",
      depthField: "depth",
      as: "resolved"
    }
  },
  {
    "$addFields": {
      "references": "$resolved",
      "metadata": [
        {
          "_metadata": "$_metadata"
        }
      ]
    }
  },
  {
    "$project": {
      "references._metadata": 0,
      
    }
  },
  {
    "$project": {
      "references": "$references",
      "merged": {
        "$concatArrays": [
          "$metadata",
          "$resolved"
        ]
      }
    }
  },
  {
    "$project": {
      results: [
        {
          merged: "$merged"
        },
        {
          references: "$references"
        }
      ]
    }
  },
  {
    "$unwind": "$results"
  },
  {
    "$facet": {
      "assest": [
        {
          "$match": {
            "results.merged": {
              "$exists": true
            }
          }
        },
        {
          "$unwind": "$results.merged"
        },
        {
          "$unwind": "$results.merged._metadata.references"
        },
        {
          "$match": {
            "results.merged._metadata.references.asssetuid": {
              "$exists": true
            }
          }
        },
        {
          "$project": {
            _id: 0,
            "asssetuid": "$results.merged._metadata.references.asssetuid"
          }
        }
      ],
      "uid": [
        {
          "$match": {
            "results.references": {
              "$exists": true
            }
          }
        },
        {
          "$unwind": "$results.references"
        },
        {
          $replaceRoot: {
            newRoot: "$results.references"
          }
        }
      ]
    }
  },
  {
    "$project": {
      "references": {
        "$concatArrays": [
          "$assest",
          "$uid"
        ]
      }
    }
  }
])

这里是测试游乐场的链接:Mongo Playground

【讨论】:

  • 谢谢,但我无法更改我的架构。是使用给定模式获取结果的任何方式。所有这些都是模式集合的一部分
  • 有什么方法可以在图形查找中显示assetuid?我也需要它,并且可以通过任何方式显示结果的嵌套视图而不是平面视图,如果无法嵌套视图,则可以显示深度意味着深度 1、深度 2 等
  • 是的,@SurajDalvi 我添加了可以显示嵌套的深度场。我也在努力获得assetuid
  • 嗨@SurajDalvi,我已经更新了查询和游乐场...这将解决引用并保留assetuid
  • 这里只有一个查询而不是 Depth 我们可以在结果中显示父 uid 吗?
猜你喜欢
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 2020-02-12
相关资源
最近更新 更多