0

Before I explain my use case, I'd like to state that yes, I could change this application so that it would store things in a different manner or even split it into 2 collections for that matter. But that's not my intention, and I'd rather want to know if this is at all possible within MongoDB (since I am quite new to MongoDB). I can for sure work around this problem if I'd really need to, but rather looking for a method to achieve what I want (no I am not being lazy here, I really want to know a way to do this).

Let's get to the problem then. I have a document like below:

{
    "_id" : ObjectId("XXXXXXXXXXXXXXXXXXXXX"),
    "userId" : "XXXXXXX",
    "licenses" : [
            {
                    "domain" : "domain1.com",
                    "addons" : [
                            {"slug" : "1"},
                            {"slug" : "2"}
                    ]
            },
            {
                    "domain" : "domain2.com",
                    "addons" : [
                            {"slug" : "1"},
                    ]
            }
    ]
}

My goal is to check if a specific domain has a specific addon. When I use the below query to count the documents with domain: domain2.com and addon slug: 2 the result should be: 0. However with the below query it returns 1. I know that this is because the query is executed document wide and not just the license index that matched domain2.com. So my question is, how to do a sub $and (or however you'd call it)?

db.test.countDocuments(
    {$and: [
        {"licenses.domain": "domain2.com"}, 
        {"licenses.addons.slug": "2"}, 
    ]}
)

Basically I am looking for something like this (below isn't working obviously), but below should return 0, not 1:

db.test.countDocuments(
    {$and: [
        {
            "licenses.domain": "domain2.com",
            $and: [
                { "licenses.addons.slug": "2"} 
            ]
        }
    ]}
)

I know there is $group and $filter operators, I have been trying many combinations to no avail. I am lost at this point, I feel like I am completely missing the logic of Mongo here. However I believe this must be relatively easy to accomplish with a single query (just not for me I guess).

I have been trying to find my answer on the official documentation and via stack overflow/google, but I really couldn't find any such use case.

Any help is greatly appreciated! Thanks :)

1 Answer 1

1

What you are describe is searching for a document whose array contains a single element that matches multiple criteria.

This is exactly what the $elemMatch operator does.

Try using this for the filter part:

{
   licenses: {
    $elemMatch: {
      domain: "domain2.com",
      "addons.slug": "2"
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for sharing this, however I just tried this and it's not working, what do you mean with "Try using this for the filter part"? The countDocuments() only takes a <query>, and <options>. Perhaps you can update your answer to reflect the complete command line? Thank you sir!
Nevermind, I believe I got it working now with the code you provided. Awesome!

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.