1

MongoDB documentation uses words like query, expression, query predicate, clause but I'm unable to find their precise meaning.

For example syntax of $elemMatch is defined as

{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }

but I don't know what does query1, query2 mean. The same article about $elemMatch mentions word query predicate:

If you specify a single query predicate in the $elemMatch expression, $elemMatch is not necessary.

But they don't specify what does it mean. Is there a place in the documentation (or in Mongo source code) where these words are precisely defined?


To be more concrete: I'm trying to understand how to form valid queries. For example why with this database

db.inventory.insertMany([
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ {value: 49} ] }
]);

following query works

db.inventory.find( {dim_cm: { $elemMatch :{ $or: [{value: 49}] } }} )

but query without $elemMatch doesn't:

db.inventory.find( {dim_cm: { $or: [{value: 49}] } })

Even though the documentation states If you specify a single query predicate in the $elemMatch expression, $elemMatch is not necessary.

Radek Micek
  • 447
  • 2
  • 9
  • I think you should post the collection and output you want instead of all these... – Ashh May 14 '18 at 18:28
  • *"but query without $elemMatch doesn't:"* - Because on a "single predicate" you simply use [`$in`](https://docs.mongodb.com/manual/reference/operator/query/in/) `db.inventory.find( {"dim_cm.value": { "$in": [49] } } }`. The only time you would use `$elemMatch` with an "inner" `$or` condition ( or `$in ) would be because there was an "array" inside the array element itself. Instead of asking questions about the manual, simply show your data and what you expect to return as a result. If you don't understand the documentation then you're probably looking at the wrong operators anyway. – Neil Lunn May 14 '18 at 21:27
  • In fact, as just pointed out you **are** looking at the wrong operators. – Neil Lunn May 14 '18 at 21:28
  • @AshishChoudhary I'm actually trying to write a library for Mongo which will make it impossible to form invalid queries - invalid query won't compile (this is strictly better than existing libraries which let you form queries which fail when you execute them). But to do so I need to understand which queries are valid and which not. So I was trying to understand documentation - which seems imprecise and sometimes wrong (like in my example where it states that `$elemMatch` is not necessary but in reality it doesn't work without it). – Radek Micek May 15 '18 at 11:37
  • @RadekMicek you can find the glossary part here https://docs.mongodb.com/manual/reference/glossary/ – Ashh May 15 '18 at 11:46

1 Answers1

-1

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. Check this link

To get better understanding of mongodb you can start from here .

Mongodb also provides free courses for new learners. Check this.

Tatkal
  • 568
  • 4
  • 9
  • 29
  • Yeah but from that documentation it's still not clear what **precisely** is query or expression. I can see these words in many parts of the documentation but I cannot find any precise explanation (I found unofficial BNF https://github.com/mongodb-js/mongodb-language-model/blob/master/docs/bnf.md but definitions there differ from official documentation). – Radek Micek May 14 '18 at 13:49
  • You will understand mongodb when you practice along with examples. Just reading the docs won't help you. – Tatkal May 15 '18 at 05:11