0

Lets suppose we have the following collection:

{
        "_id" : 10,
        "youtube_id" : "xxx",
        "title" : "yyy",
        "counters" : [
                {
                        "timestamp" : 202003181300,
                        "views" : 7433,
                        "likes" : 2477,
                        "dislikes" : 34,
                        "comments" : 105
                },
                {
                        "timestamp" : 202003200815,
                        "views" : 12494,
                        "likes" : 4877,
                        "dislikes" : 248,
                        "comments" : 322
                }
        ]
}
{
        "_id" : 11,
        "youtube_id" : "xxz",
        "title" : "yyz",
        "counters" : [
                {
                        "timestamp" : 202003181300,
                        "views" : 12,
                        "likes" : 2,
                        "dislikes" : 0,
                        "comments" : 0
                },
                {
                        "timestamp" : 202003200815,
                        "views" : 198,
                        "likes" : 50,
                        "dislikes" : 4,
                        "comments" : 36
                }
        ]
}

And I want to query for the number of comments given a certain timestamp and title, my guess would be to do as follows:

db.youtube.find({"title":"yyy","counters.timestamp":{$eq:202003181300}},{"_id":0,"title":1,"counters.timestamp":1,"counters.comments":1}).pretty()

And the result I am getting is the following:

{
        "title" : "yyy",
        "counters" : [
                {
                        "timestamp" : 202003181300,
                        "comments" : 105
                },
                {
                        "timestamp" : 202003200815,
                        "comments" : 322
                }
        ]
}

The problem is that I would only like to see the part that has the same timestamp I have done the query for, like this:

{
        "title" : "yyy",
        "counters" : [
                {
                        "timestamp" : 202003181300,
                        "comments" : 105
                }
        ]
}

Is there a way to do it, and in case I am missinterpreting what the query does could anyone give me a piece of advice? Thanks in advance

Ps. It is my first post, and I am learning the mongo basics so forgive me if I have said anything wrong.

BizarroJr
  • 23
  • 6
  • 1
    Yes! Really thankful for that, I dind't came across the post you linked earlier and it certainly worked like a charm. I'm new to mongo and the queries and it's a hell of ride every time I get stuck on an error. Again, thanks for you advice. – BizarroJr Apr 12 '20 at 19:59

1 Answers1

0

The problem is that your "counters.timestamp":{$eq:202003181300} gets ignored, since counters is an array of subdocuments, not a subdocument itself. You can use $elemMatch query modifier like I did here:

    db.youtube.find(
      {
        "title":"yyy","counters":{
          $elemMatch: { "timestamp": { $eq: 202003181300 } }
        }
      },
      {"_id": 0, "title": 1, "counters.timestamp": 1, "counters.comments": 1 }
    ).pretty()

P.S. You can omit $eq: query modifier and simply do { "timestamp": 202003181300 }. Let me know if it works.

unsame
  • 21
  • 1
  • 6