MongoDB中Array数组的项目元素

问题描述 投票:0回答:1

我目前正在努力搜索导入MongoDB指南针的JSON文件中的数组数组。在下面的代码中,您可以看到数组“track”包含多个其他数组。现在我想获得每个数组的第4个值,例如,我想从第一个“加载”,从第二个“鼠标悬停”等等......

{
"name":"nutzer1_50ms",
"ip":"::1",
"date":"Sat, 16 Dec 2017 21:53:19 +0100",
"screen":"1920x1080",
"window":"1920x966",
"document":"1024x786",
"track":[
[1513457569930,0,0,"load",""],
[1513457569953,79,229,"mouseover","p3"],
[1513457570274,79,228,"mousemove","p3"],
[1513457570280,79,226,"mousemove","p3"],
["end","end","end","end"]]
}

我很乐意得到一些帮助:)

arrays mongodb mongodb-query aggregation-framework
1个回答
1
投票

您可以尝试以下聚合查询。

$map迭代track值和$arrayElemAt来投射数组中的第4个元素。

db.collection_name.aggregate([
  {
    "$project": {
      "4th": {
        "$map": {
          "input": "$track",
          "as": "track",
          "in": {
            "$arrayElemAt": [
              "$$track",
              3
            ]
          }
        }
      }
    }
  }
])
© www.soinside.com 2019 - 2024. All rights reserved.