0

First of all please correct the question's title to appropriate one - it was hard for me to find a short and descriptive title to the question below.

Lets say I have a list of documents like ones below:

[
    cdate: { $date: ""2015-09-15T05:53:04.656Z" },
    factor1: "aaa",
    factor2: "bbb",
    extra_info: [
        { host: "ex.com", int_value: 10 },
        { host: "ms.com", int_value: 20 },
        { host: "apple.com", int_value: 5 },
        ..
    ]
]

extra_info array size varies. I need to get all documents having factor1="something", factor2="something else" and one of extra_info's array's host should be equal to "ms.com". The result should be presented as:

[
    cdate: { $date: ""2015-09-15T05:53:04.656Z" },
    factor1: "something",
    factor2: "something else",
    host: "ms.com",
    int_value: 5
],
[
    cdate: { $date: ""2015-09-14T08:17:32.123Z" },
    factor1: "something",
    factor2: "something else",
    host: "ms.com",
    int_value: 14
],
...

The question is how to do it?

One ways is that I can get all the documents and then manually filter out extra_info array, leaving only values that I need, but is there any way to stick it in under one query?

kaytrance
  • 2,657
  • 4
  • 30
  • 49
  • Sorry. My bad. You are right. Positional operator is only for updating. –  Sep 15 '15 at 13:16
  • You can use: http://docs.mongodb.org/master/reference/operator/query/elemMatch/ –  Sep 15 '15 at 13:21
  • yes, elemMatch will be used to filter out only documents containing certain `host`, but the result of the query will contain all `extra_info` array anyway, which I then need to look through. This is a two step solution, but the question was is there any clever/geeky way to achieve result? Like using aggregate or map-reduce.. – kaytrance Sep 15 '15 at 13:28

0 Answers0