Now I want those result whose “test” field has property other than “a” or in another way, I want those objects which have multiple keys/properties in a “test” field or to check the size of an object greater than 1
Hi @Prasad_Saya, Thanks for your response. But I am not expecting that. $exists will return if the key is present or not. What I want is I would like to get that any other exists than given key in object field or I want is if that object has multiple keys or not i.e size of object length should be greater than 1.
So, for that, we used $gt operator. So, is that the right thing to do, Any downside of doing as we want to leverage indexes as well?
You have to write an aggregation query and use the $objectToArray aggregation operator to convert the object as an array and then find the length of the array. If the length of the array is greater than 1, then there are more than one field in the object. See the example from the documentation explains how to convert the object to an array.
Okay thanks, @prasad. Using above, if I have index on ‘test’ field. So, will it leverage that or will this have any performance impact on querying documents
@Aamod_Pisat, I am not sure about that (I think it will not use the index). You can check if a query is using an index or not by using the explain on the query. The explain method generates a query plan which will have information about index usage by the query.
HI @Prasad_Saya, We tried working with aggregation query using $objectToArray but the query doesn’t use indexes as it’s taking COLSCAN even though I have an index on that field.
But below query which I shared is taking index i.e. IXSCAN. So, hopefully, there won’t be any downside to doing this.
Hello @Aamod_Pisat, as I had mentioned earlier, you cannot use that query to count the number of fields in the test object (or sub-document ). The query you had posted doesn’t the number of fields - it only finds documents where the "test.a" value is greater than1.
Yes, the index defined on the "test.a" will be used in the query - as you had mentioned there is an IXSCAN from the query plan.
Please note that the two queries have different purposes.
Hi @Aamod_Pisat, Mongo stores json objects as bson, and so, the way to measure this value is using the key $bsonSize where the minimum size for an empty object is 5. Below is an example of…