Needed to use $all to find array instances with two elements in any order

In the lecture on finding documents according to some criteria on the members of the array, a find operation for any document that contains an array with two specific elements somewhere in the array uses a simple syntax that I could not duplicate and have work in the shell or in Compass. Instead, I had to use $all: [, ] after the array name:. Am I missing something, or has the syntax changed? (I like the $all: better, it is more explicit, less terse and unreadable except by those in the know).

Hi @William_Frank_90755,

Can you please share the query that you tried to execute and also share the output if you got any ?

You can definitely write more than one type of query to get the same result. What’s important here is which one among those is computationally cheaper.

Thanks,
Shubham Ranjan
Curriculum Support Engineer

Thanks for your response, Shubham. I can’t find the part of the lecture where it shows a find of the documents that contain an array that in turn contains both elementa and elementb. And, my command window was closed, so I can’t find my history. I tried every alternative to what was displayed in the lecture that I could imagine. They all told me there was a syntax error.

If possible, could you send me your recommend way to do a find from document d that contains array a that contains at least the two elements e1 and e2, that is simpler and more efficient than using $all. Or, where in a chapter 2 lecture it illustrates this. Also, would be quite interested to learn why this alternative is more efficient.

Hi @William_Frank_90755,

Are you talking about this video lecture Lecture: Reading Documents: Array Fields ?

If yes, then at 2:11 we have used this query

 db.movies.find({cast:["Jeff Bridges", "Tim Robbins"]}).pretty() 

This is a query based on an equality condition. We are basically returning only those documents in which the cast field is exactly equal to this ["Jeff Bridges", "Tim Robbins"].

Now Let’s take an example, we have a document like this in our collection.

{
"name" : "Shubham",
"groups" : ["United", "Kings XI", "Manchester", "Dare Devils"],
...
...
}

Now we want to return all those documents in which the groups field contains the value United and Manchester.

You can write the query using both the $all and $and operator.

db.col.find({"groups": {$all: ["United", "Manchester"]}})
db.col.find({$and: [{"groups":"United"}, {"groups":"Manchester"}]})

On the surface, it may look like two different query but the implementation of both these queries will be same. If you do not have index on the groups field then it is going to be a Collection Scan. This is what the parsedQuery will look like for both the queries above.

"parsedQuery": {
      "$and": [
        {
          "groups": {
            "$eq": "United"
          }
        },
        {
          "groups": {
            "$eq": "Manchester"
          }
        }
      ]
    }

In this particular case, there would not be much difference in the performance of these two queries as they have the same implementation internally.

If you want to understand the performance of any query then please use the cursor.explain() method.

Hope it helps!

If you have any other query then please feel free to get back to us.

Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

1 Like


Shubham_Ranjan

      MongoDB Staff: Curriculum Support Engineer




    October 22

Hi @William_Frank_90755,

Are you talking about this video lecture Lecture: Reading Documents: Array Fields ?

If yes, then at 2:11 we have used this query

 db.movies.find({cast:["Jeff Bridges", "Tim Robbins"]}).pretty()

This is a query based on an equality condition. We are basically returning only those documents in which the cast field is exactly equal to this ["Jeff Bridges", "Tim Robbins"].

Right, this query raised no problems, except of course I had to quote the cast- :“cast”:

William_Frank_90755:
If possible, could you send me your recommend way to do a find from document d that contains array a that contains at least the two elements e1 and e2, that is simpler and more efficient than using $all. Or, where in a chapter 2 lecture it illustrates this. Also, would be quite interested to learn why this alternative is more efficient.

Now Let’s take an example, we have a document like this in our collection.


{
"name" : "Shubham",
"groups" : ["United", "Kings XI", "Manchester", "Dare Devils"],
...
...
}

Now we want to return all those documents in which the groups field contains the value United and Manchester.

You can write the query using both the $all and $and operator.

db.col.find({"groups": {$all: ["United", "Manchester"]}})
db.col.find({$and: [{"groups":"United"}, {"groups":"Manchester"}]})

On the surface, it may look like two different query but the implementation of both these queries will be same. If you do not have index on the groups field then it is going to be a Collection Scan. This is what the parsedQuery will look like for both the queries above.


      "parsedQuery": {
"$and": [
{
"groups": {
"$eq": "United"
}
},
{
"groups": {
"$eq": "Manchester"
}
}
]
}

In this particular case, there would not be much difference in the performance of these two queries as they have the same implementation internally.

Excellent.

If you want to understand the performance of any query then please use the cursor.explain() method.

Yes, know about that.

Hope it helps!

Indeed. Shubham, you are really an immense help. I am really surprised and delighted by the excellence of this course and your willingness to go the extra step. Also, I must say, that I really love Mongodb!

Hi @William_Frank_90755,

I’m glad you are finding the content and our support helpful. Feel free to reach out to us if you have any queries.

Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer