bux
bux

Reputation: 7739

ALL query operator with range?

Is it possible to specify a range when we use all operator in query ?

Example:

Where favorites_ordered contains [174, 225, 25, 165, 65, 87, 158], standart way will be:

Select where 174 and 158 are in favorites_ordered field.

db.inventory.find( { favorites_ordered: { $all: [ 174, 158 ] } } )

[174, 225, 25, 165, 65, 87, 158] : found

I would like make

Select where 174 and 158 are in favorites_ordered field and are in 3 first values.

[174, 225, 25, 165, 65, 87, 158] : not found

Upvotes: 0

Views: 53

Answers (1)

Pierre-Louis Gottfrois
Pierre-Louis Gottfrois

Reputation: 17631

You can do this in mongo:

> db.foo.insert({favorites_ordered: [174, 225, 25, 165, 65, 87, 158]})
> db.foo.insert({favorites_ordered: [158, 225, 25, 165, 65, 87, 174]})
> db.foo.insert({favorites_ordered: [100, 158, 225, 25, 165, 65, 87, 174]})

> db.foo.find({ 'favorites_ordered.0': { $in : [174, 158] } })
{ "_id" : ObjectId("51fa394f59a0a6afeec5b138"), "favorites_ordered" : [  174,  225,  25,  165,  65,  87,  158 ] }
{ "_id" : ObjectId("51fa3a2d59a0a6afeec5b139"), "favorites_ordered" : [  158,  225,  25,  165,  65,  87,  174 ] }

Upvotes: 1

Related Questions