In the last post we started running queries on MongoDB. To continue on the operators, I decided to do a find on the auto generated id:
There is also the not equal operator:
We saw the and operator and the or operator in the earlier post. Next is the not operator:
There is also a nor operator. The below query will fetch documents colors are not red nor their rating a 4.
> db.fruits.find({ "_id" : ObjectId("52d49575e3a91584ad8f614e")})
{ "_id" : ObjectId("52d49575e3a91584ad8f614e"), "name" : "mango", "color" : "yel
low", "rating" : 2, "weight" : 1.03 }
There are also comparison operators available for use in queries:
> db.fruits.find( { rating: { $gt: 0 } } )
{ "_id" : ObjectId("52d305ab4e386a0b85ac249a"), "name" : "apple", "color" : "red
", "rating" : 4, "weight" : 2.3 }
{ "_id" : ObjectId("52d49575e3a91584ad8f614e"), "name" : "mango", "color" : "yel
low", "rating" : 2, "weight" : 1.03 }
Other operators are $gte,$lt and $lte. Similar to the $in operator, we can also try the $nin operator:
> db.fruits.find( { rating: { $nin: [1,2,3] } } )
{ "_id" : ObjectId("52d305ab4e386a0b85ac249a"), "name" : "apple", "color" : "red
", "rating" : 4, "weight" : 2.3 }
{ "_id" : ObjectId("52d30d464e386a0b85ac249b"), "name" : "lost", "age" : 12 }
{ "_id" : ObjectId("52d30d524e386a0b85ac249c"), "name" : "INS Viraat", "wt" : 50
00, "displacement" : 190.87 }
>
This is interesting. The records returned were those that did not match as well as those that did not have the specified attribute.There is also the not equal operator:
> db.fruits.find( { rating: { $ne: 1 } } )
{ "_id" : ObjectId("52d305ab4e386a0b85ac249a"), "name" : "apple", "color" : "red
", "rating" : 4, "weight" : 2.3 }
{ "_id" : ObjectId("52d30d464e386a0b85ac249b"), "name" : "lost", "age" : 12 }
{ "_id" : ObjectId("52d30d524e386a0b85ac249c"), "name" : "INS Viraat", "wt" : 50
00, "displacement" : 190.87 }
{ "_id" : ObjectId("52d49575e3a91584ad8f614e"), "name" : "mango", "color" : "yel
low", "rating" : 2, "weight" : 1.03 }
>
The ne operator also returns records that do not possess the specified attribute.We saw the and operator and the or operator in the earlier post. Next is the not operator:
> db.fruits.find( { weight: { $not: { $gt: 1.00}}})
{ "_id" : ObjectId("52d30d464e386a0b85ac249b"), "name" : "lost", "age" : 12 }
{ "_id" : ObjectId("52d30d524e386a0b85ac249c"), "name" : "INS Viraat", "wt" : 50
00, "displacement" : 190.87 }
>
This is an interesting point from the docs:
{ $not: { $gt: 1.99 } } is different from the $lte operator. { $lte: 1.99 } returns only the documents where --- field exists and its value is less than or equal to 1.99.As we see, the records returned do not even have the weight field.
There is also a nor operator. The below query will fetch documents colors are not red nor their rating a 4.
> db.fruits.find( { $nor: [ { color: 'red' }, { rating: 4 } ] } )
{ "_id" : ObjectId("52d30d464e386a0b85ac249b"), "name" : "lost", "age" : 12 }
{ "_id" : ObjectId("52d30d524e386a0b85ac249c"), "name" : "INS Viraat", "wt" : 50
00, "displacement" : 190.87 }
{ "_id" : ObjectId("52d49575e3a91584ad8f614e"), "name" : "mango", "color" : "yel
low", "rating" : 2, "weight" : 1.03 }
>
We specified the multiple conditions using an array.
No comments:
Post a Comment