0

My documents look like this:

{
    "_id" : ObjectId("58b714f753e4a2105c002416"),
    "routes" : [ 
        {
            "legs" : [ 
                {
                    "duration" : {
                        "text" : "6 mins",
                        "value" : 348
                    },
                    "traffic_speed_entry" : [],
                }
            ],
            "warnings" : [],
        }
    ],
    "time" : "01/03/2017 18:37:43"
}

How can I project a table with time and duration (one line per array element) only?

The best I could get without errors was this:

db.getCollection('testcoll').find({}, {time:1, routes: 1})

1 Answer 1

2

You will need to use aggregation to flatten the array and project the values.

db.getCollection('testcoll').aggregate({
    $unwind: "$routes"
}, {
    $unwind: "$routes.legs"
}, {
    $project: {
        time: 1,
        duration: "$routes.legs.duration",
        _id: 0
    }
})
Sign up to request clarification or add additional context in comments.

Comments

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.