0

I am currently struggling with a search through an array of arrays within a JSON file that is imported into MongoDB compass. At the code below you can see the array "track" contains multiple other arrays. Now I want to get the 4th value of each of this arrays, for example, I want to get "load" from the first, "mouseover" from the second and so on...

{
"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"]]
}

I would be very glad to get some help on this :)

1 Answer 1

1

You can try below aggregation query.

$map iterating over track values & $arrayElemAt to project the 4th element from array.

db.collection_name.aggregate([
  {
    "$project": {
      "4th": {
        "$map": {
          "input": "$track",
          "as": "track",
          "in": {
            "$arrayElemAt": [
              "$$track",
              3
            ]
          }
        }
      }
    }
  }
])
Sign up to request clarification or add additional context in comments.

7 Comments

thx first of all, I'm trying to use this code but it won´t work :(
np. Can I see your query ? Which part is not working ?
I am taking this straight into MongDB Compass Filter: {"$project": {"4th": {"$map": {"input": "$track","as": "track","in": {"$arrayElemAt": ["$$track",3]}}}}}
Query Filter seems like for a regular query. This is a aggregate query. Not sure where/how run aggregate query in compass.
Ok thanks anyway, I need to get the data visualized there :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.