1

I have a collection with following document type:

{
 field1: 1,
 field2: "test1",
 field3: [1,2,3]
}

Suppose I have the following documents in my collection (ignoring field1 and field2 and only considering field3)

Collection:

doc1-  {...
        field3: [3,4,5,6]
       }
doc2-  {...
        field3: [3,4]
       }
doc3-  {...
        field3: [1,2,3]
       }
doc4-  {...
        field3: [3,6,7]
       }

Now, I am querying on this collection on field3 with values [3,4,6,7].

So, I execute a query in the collection in such a way that I should get all documents that have all its array elements present in the query array. In my case, it should return the documents doc2 and doc4, since all its elements of field3 are existing in the query, but Query.ALL doesn't work.

How can I accomplish this in MongoDB querying?

(edited for more clarity)

Why $all does'nt work?

EX: If my questioning array is [3,4], when I use Query.All("field3",new BsonValue[]{3,4}), I will get all the documents with "field3" having all the elements in the questioning array i.e., 3 and 4. So In such case my result docs would be doc1 and doc2 from example above. But my requirement is only doc2. Because all the elements of my document should be there in my query array and not the other way.

Why $in does'nt work? If atleast one element of the quetiong array is present in the document it is retrieved. Ex: questioning array is [3,4], when I use Query.In("field3",new BsonValue[]{3,4}), result will be all 4 documents of collection doc1, doc2, doc3 and doc4

1
  • Maybe your question has already been asked here. Commented Nov 20, 2013 at 12:33

1 Answer 1

1

Take a look at this question.

The operator All in the C# driver or $all in MongoDb considers all the elements, as explained here in the documentation:

The $all operator selects the documents where the value of a field is an array that contains all the specified elements.

Then, supposing the elements 3 and 4, the operators considers arrays, with this logic: the array contains elements 3 AND 4.

I think you should use then the Query.In (further info about $in here) if you want to consider arrays with 3 OR 4.

UPDATE

After your clarification, I noticed this request in the MongoDb's site. In that page, the user requested a feature in order to retrieve only the documents with an array that contains only specified elements, but this is not possible actually.

Unfortunately, I think, this is a limitation of MongoDb and, in general, of NoSQL databases.

Sign up to request clarification or add additional context in comments.

4 Comments

Query.All does not work in my as I already mentioned. As $all operator retrieves documents which has all the array elements contained in the questioning array.
..(Contd to previous) EX: If my questioning array is [3,4], when I use Query.All("field3",new BsonValue[]{3,4}), I will get all the documents with "field3" having all the elements in the questioning array i.e., 3 and 4. So In such case my result docs would be doc1 and doc2 from example above. But my requirement is only doc2. Because all the elements of my document should be there in my query array
@SarikaKondra That's what my answer explains. In the question, you wrote "In my case, it should return the documents doc2 and doc4, since all its elements of field3 are existing in the query,", and that's what Query.All does, as the documentation explains and such as you are saying in your comment. I answered to your question without knowing what your requirement was. Query.All retrieves the documents containing ALSO the elements in the questioning array. You need then to improve the query to find only doc2, that means is the document containing only the elements in the questioning array.
@SarikaKondra Indeed, in the documentation there's an example : "Use $all to Match Values", with this description: "The following operation uses the $all operator to query the inventory collection for documents where the value of the tags field is an array whose elements INCLUDE appliance, school, and book".

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.