2014년 12월 6일 토요일

MongoDB querying for Embeded Document

I have a mongo document which has structure like


{
"_id" : "THIS_IS_A_DHP_USER_ID+2014-11-26",
"_class" : "weight",
"items" : [
    {
        "dateTime" : ISODate("2014-11-26T08:08:38.716Z"),
        "value" : 98.5
    },
    {
        "dateTime" : ISODate("2014-11-26T08:18:38.716Z"),
        "value" : 95.5
    },
    {
        "dateTime" : ISODate("2014-11-26T08:28:38.663Z"),
        "value" : 90.5
    }
],
"source" : "MANUAL",
"to" : ISODate("2014-11-26T08:08:38.716Z"),
"from" : ISODate("2014-11-26T08:08:38.716Z"),
"userId" : "THIS_IS_A_DHP_USER_ID",
"createdDate" : ISODate("2014-11-26T08:38:38.776Z")
}
{
"_id" : "THIS_IS_A_DHP_USER_ID+2014-11-25",
"_class" : "weight",
"items" : [
    {
        "dateTime" : ISODate("2014-11-25T08:08:38.716Z"),
        "value" : 198.5
    },
    {
        "dateTime" : ISODate("2014-11-25T08:18:38.716Z"),
        "value" : 195.5
    },
    {
        "dateTime" : ISODate("2014-11-25T08:28:38.716Z"),
        "value" : 190.5
    }
],
"source" : "MANUAL",
"to" : ISODate("2014-11-25T08:08:38.716Z"),
"from" : ISODate("2014-11-25T08:08:38.716Z"),
"userId" : "THIS_IS_A_DHP_USER_ID",
"createdDate" : ISODate("2014-11-26T08:38:38.893Z")
}


I have a mongo document which has structure like
{
"_id" : "THIS_IS_A_DHP_USER_ID+2014-11-26",
"_class" : "weight",
"items" : [
    {
        "dateTime" : ISODate("2014-11-26T08:08:38.716Z"),
        "value" : 98.5
    },
    {
        "dateTime" : ISODate("2014-11-26T08:18:38.716Z"),
        "value" : 95.5
    },
    {
        "dateTime" : ISODate("2014-11-26T08:28:38.663Z"),
        "value" : 90.5
    }
],
"source" : "MANUAL",
"to" : ISODate("2014-11-26T08:08:38.716Z"),
"from" : ISODate("2014-11-26T08:08:38.716Z"),
"userId" : "THIS_IS_A_DHP_USER_ID",
"createdDate" : ISODate("2014-11-26T08:38:38.776Z")
}
{
"_id" : "THIS_IS_A_DHP_USER_ID+2014-11-25",
"_class" : "weight",
"items" : [
    {
        "dateTime" : ISODate("2014-11-25T08:08:38.716Z"),
        "value" : 198.5
    },
    {
        "dateTime" : ISODate("2014-11-25T08:18:38.716Z"),
        "value" : 195.5
    },
    {
        "dateTime" : ISODate("2014-11-25T08:28:38.716Z"),
        "value" : 190.5
    }
],
"source" : "MANUAL",
"to" : ISODate("2014-11-25T08:08:38.716Z"),
"from" : ISODate("2014-11-25T08:08:38.716Z"),
"userId" : "THIS_IS_A_DHP_USER_ID",
"createdDate" : ISODate("2014-11-26T08:38:38.893Z")
}

The query that want to fire on this document structure, is for finding documents for a particular user id, unwinding the the embedded array and getting the objects that fall between a particular date range.
Can an aggregation created for such a query or else is this need be solved using map-reduce kind of querying.



You can do this easily with aggregation using $match $unwind and $match.

You definitely don't need mapReduce.


댓글 없음:

댓글 쓰기