无法获取 mongodb 查询中未知键的数据

发布于 2025-01-11 22:05:56 字数 197 浏览 1 评论 0原文

我想使用 mongodb 查询从文档中获取子数组,但我不知道它的键。我的数据看起来像这样:

{ 'log file name I don't Know': [Array] }

'log file name I don't Know' 是该级别的唯一键。 mongodb 聚合管道中是否有类似“获取第一个键的值”之类的内容?

I want to get a subarray from a document using mongodb queries but I don't know the key for it. My data looks something like this:

{ 'log file name I dont know': [Array] }

'log file name I dont know' is the unique key at that level. Is there something like "get value for the first key" in mongodb aggregation pipeline?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2025-01-18 22:05:56

1 也许是这样的:

db.collection.aggregate([
{
  $group: {
    _id: "$_id",
    new: {
      $first: "$ROOT"
    }
  }
},
{
  "$project": {
    "map": {
      "$objectToArray": "$new"
    }
  }
},
{
  $unwind: "$map"
},
{
  $match: {
    "map.k": {
      "$ne": "_id"
   },
      "map.v": 4
  }
 }
])

解释:

  1. 将根文档添加到名为“new”的新键
  2. 将名为“new”的对象转换为名为“map”的数组
  3. $unwind the array
  4. 删除带有 k:"_id" 的数组元素,因此在最终结果只有:
    地图.k ->未知的钥匙
    地图.v ->未知键的数组值

最后,您可以在 $match 阶段添加搜索到的元素,在示例中:"map.v":4

playground1


2 当你想在比赛阶段后保留未知键的奇怪结构时,可以选择以下选项:

  db.collection.aggregate([
  {
    $group: {
      _id: "$_id",
      new: {
        $first: "$ROOT"
       }
     }
  },
  {
    "$project": {
      "map": {
        "$objectToArray": "$new"
       }
     }
  },
  {
    $match: {
     "map.v": 1
  }
  },
  {
    "$project": {
      "map": {
       "$arrayToObject": "$map"
   }
  }
  },
  {
     $replaceRoot: {
        newRoot: "$map"
     }
  }
  ])

解释:

  1. 将根文档添加到名为“new”的新键
  2. 转换名为“的对象 ” “new”到名为“map”的数组
  3. 匹配必要的文档(在示例中这是数组元素1(“map.v”:1)
  4. 将数组转换回对象。
  5. 用“map docuent”替换根文档,这样它看起来像原始集合

playground2

几句话:文件名值不建议作为键,最佳实践是文件名是键/值,因此如果您想搜索文件名值,则可以在该文件名键字段上创建索引,因此您的 json 结构如下所示:

  { "filename":"The unknown file name"  , "theArray":[1,2,3] }

1 Maybe something like this:

db.collection.aggregate([
{
  $group: {
    _id: "$_id",
    new: {
      $first: "$ROOT"
    }
  }
},
{
  "$project": {
    "map": {
      "$objectToArray": "$new"
    }
  }
},
{
  $unwind: "$map"
},
{
  $match: {
    "map.k": {
      "$ne": "_id"
   },
      "map.v": 4
  }
 }
])

Explained:

  1. Add the root document to new key named "new"
  2. Convert the object named "new" to array named "map"
  3. $unwind the array
  4. Remove the array elements with k:"_id" , so in the final result to have only:
    map.k -> the unknown keys
    map.v -> the array values for the unknown keys

Finally you can add in the $match stage the searched element , in the example:"map.v":4

playground1


2 Here is the option when you want to preserve the strange structure with the unknown key after the match stage:

  db.collection.aggregate([
  {
    $group: {
      _id: "$_id",
      new: {
        $first: "$ROOT"
       }
     }
  },
  {
    "$project": {
      "map": {
        "$objectToArray": "$new"
       }
     }
  },
  {
    $match: {
     "map.v": 1
  }
  },
  {
    "$project": {
      "map": {
       "$arrayToObject": "$map"
   }
  }
  },
  {
     $replaceRoot: {
        newRoot: "$map"
     }
  }
  ])

Explained:

  1. Add the root document to new key named "new"
  2. Convert the object named "new" to array named "map"
  3. Match the necessary documents(in the example this is array element 1 ( "map.v":1 )
  4. Convert back the array to object.
  5. Replace the root tocument with the "map docunent" so it looks as original collection

playground2

Few words: File name value is not recommended to be a key , best practice is the filename to be key/value so if you want to search on filename value to be possible to create index on that filename key field , so your json structure to looke something like:

  { "filename":"The unknown file name"  , "theArray":[1,2,3] }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文