Mongo $ and selector

How does $ mango and selector work? I am having trouble returning the correct results.

Let's say I have a collection of something like this:

{ "_id" : "F7mdaZC2eBQDXA5wx", "quantity" : 5 }
{ "_id" : "F7mdaZC2eBQDXA5wx", "quantity" : 9 }
{ "_id" : "F7mdaZC2eBQDXA5wx", "quantity" : 34 }
{ "_id" : "F7mdaZC2eBQDXA5wx", "quantity" : 66 }

and I run the query:

var selectorMin = 9;
var selectorMax = 42;

ScrapReport.find({ $and: [ { quantity: { $gte: selectorMin }, quantity: { $lte: selectorMax } } ] })

I expected the mongo to return only 9 and 34 to me. But for some reason it also returns 5 and 66.

What happened to my request?

+4
source share
3 answers

, , quantity >= 9 .. 9, 34 66 , quantity <= 42 ie 34, 9 5. , , , ..

Documents which satisfy "quantity >= 9"
+
Documents which satisfy "quantity <= 42"

not

Documents which satisfy "9 <= quantity <= 42"

ScrapReport.find({ "quantity": { "$gte": selectorMin, "$lte": selectorMax } })

, MongoDB i.e.

9 <= quantity <= 42

, , $and, .

+4

, , . . , , ?

? , , ? , , , , , . AND?

AND.

{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

AND , , AND.

. , , AND . - . . . , .

$and

{ $and: [ { quantity: { $gte: selectorMin }, quantity: { $lte: selectorMax } } ] }

, 1 quantity: { $gte: selectorMin } 2 quantity: { $lte: selectorMax }. AND 9 34. , , - . . {}. ? , AND . AND , .

,

{ $and: [ { <expression1> } ] }

.

ScrapReport.find({ $and: [ { quantity: { $gte: selectorMin } }, { quantity: { $lte: selectorMax } } ] })

? , , .

, , Mongo , .

ScrapReport.find({ quantity: 9 })

? , Mongo , 9, . . .

ScrapReport.find({ quantity: 9, quantity: 5 })

? , ? , . 5. !

ScrapReport.find({ quantity: 9, quantity: 5, quantity: 34 })

? , 34. . , -

, , .

. , quantity: { $gte: selectorMin } quantity: { $lte: selectorMax }. , . quantity: { $lte: selectorMax }. 3 5, 9 34.

, .. quantity: { $lte: selectorMax }, quantity: { $gte: selectorMin }, quantity: { $gte: selectorMin }. 3 9, 34 66.

,

ScrapReport.find({ quantity: { $gte: selectorMin }, quantity: { $lte: selectorMax } })

, .

. , .

+3

:

:

ScrapReport.find( { "$and": [{ "quantity": { "$lte": selectorMax } } ] } )

:

ScrapReport.find( { "quantity": { "$lte": selectorMax } } )

, JSON , .

, , "" selectorMax.

@chridam, :

ScrapReport.find({ "quantity": { "$gte": selectorMin, "$lte": selectorMax } })
+1
source

Source: https://habr.com/ru/post/1628464/


All Articles