Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k points)

I have a MongoDB collection with documents in the following format:

"_id" : ObjectId("4e8ae86d08101908e1000001"), 

"name" : ["Name"], 

"zipcode" : ["2223"] } 

"_id" : ObjectId("4e8ae86d08101908e1000002"),

 "name" : ["Another ", "Name"],

 "zipcode" : ["2224"] 

}

I can currently get documents that match a specific array size:

db.accommodations.find({ name : { $size : 2 }})

This correctly returns the documents with 2 elements in the name array. However, I can't do a $gt command to return all documents where the name field has an array size of greater than 2:

db.accommodations.find({ name : { $size: { $gt : 1 } }})

How can I select all documents with a name array of a size greater than one (preferably without having to modify the current data structure)?

1 Answer

0 votes
by (106k points)

The query for documents where array size is greater than 1 can be written using  $where:-

db.accommodations.find( { $where: "this.name.length > 1" } );

Related questions

0 votes
1 answer
asked Feb 2, 2020 in Web Technology by ashely (50.2k points)
0 votes
1 answer
0 votes
3 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...