A
A
artem782016-09-18 17:56:43
NoSQL
artem78, 2016-09-18 17:56:43

How to select from document field which is an array in MongoDB?

There is a collection with objects of this kind:

{
    "_id": ObjectId("57dea6ec32d8c016fa9377d0"),
    "url": "http://site1.ru",
    "checks": [{
        "date": ISODate("2016-09-18T10:20:30Z"),
        "result": 5
    }, {
        "date": ISODate("2016-09-15T20:05:05Z"),
        "result": 1
    }, {
        "date": ISODate("2016-09-10T17:30:10Z"),
        "result": 7
    }]
}

{
    "_id": ObjectId("57dea70032d8c016fa9377d1"),
    "url": "http://site2.ru",
    "checks": [{
        "date": ISODate("2016-09-14T12:13:14Z"),
        "result": 3
    }, {
        "date": ISODate("2016-09-05T15:16:17Z"),
        "result": 0
    }, {
        "date": ISODate("2016-09-03T18:19:20Z"),
        "result": 2
    }]
}

I just recently started learning MongoDB. Tell me how you can write such 2 queries:
1) Display the dates of all checks of all sites "in one heap" sorted by date
2) Display the average value of the result field with a breakdown by day (also for all sites)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-09-18
@artem78

aggregation framework, unwind, group

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question