Skip to content

Instantly share code, notes, and snippets.

@ywwwtseng
Created December 12, 2017 08:05
Show Gist options
  • Save ywwwtseng/d4f9b35c94a3b80023b9132fdc381af0 to your computer and use it in GitHub Desktop.
Save ywwwtseng/d4f9b35c94a3b80023b9132fdc381af0 to your computer and use it in GitHub Desktop.
Mongodb practice

Mongodb practice

Array Query Operators > $elemMatch (query)

Given the following documents in the scores collection:

{ _id: 1, results: [ 82, 85, 88 ] }
{ _id: 2, results: [ 75, 88, 89 ] }

The following query matches only those documents where the results array contains at least one element that is both greater than or equal to 80 and is less than 85.

db.scores.find(
   { results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)

The query returns the following document since the element 82 is both greater than or equal to 80 and is less than 85

{ "_id" : 1, "results" : [ 82, 85, 88 ] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment