Reputation: 141
I am new to mongo db and trying to understand how to query a db. I was reading a tutorial from this link http://code.tutsplus.com/tutorials/getting-started-with-mongodb-part-1--net-22879
Following this tutorial I created a simple db like below,
db.nettuts.insert({
first: 'matthew',
last: 'setter',
dob: '21/04/1978',
gender: 'm',
hair_colour: 'brown',
occupation: 'developer',
nationality: 'australian'
});
db.nettuts.insert({
first: 'james',
last: 'caan',
dob: '26/03/1940',
gender: 'm',
hair_colour: 'brown',
occupation: 'actor',
nationality: 'american'
});
db.nettuts.insert({
first: 'arnold',
last: 'schwarzenegger',
dob: '03/06/1925',
gender: 'm',
hair_colour: 'brown',
occupation: 'actor',
nationality: 'american'
});
db.nettuts.insert({
first: 'tony',
last: 'curtis',
dob: '21/04/1978',
gender: 'm',
hair_colour: 'brown',
occupation: 'developer',
nationality: 'american'
});
db.nettuts.insert({
first: 'jamie lee',
last: 'curtis',
dob: '22/11/1958',
gender: 'f',
hair_colour: 'brown',
occupation: 'actor',
nationality: 'american'
});
db.nettuts.insert({
first: 'michael',
last: 'caine',
dob: '14/03/1933',
gender: 'm',
hair_colour: 'brown',
occupation: 'actor',
nationality: 'english'
});
db.nettuts.insert({
first: 'judi',
last: 'dench',
dob: '09/12/1934',
gender: 'f',
hair_colour: 'white',
occupation: 'actress',
nationality: 'english'
});
and was trying to use querys like this below
db.nettuts.find({gender: 'm'});
which returned all the actors who are male.
When I tried the command
db.nettuts.find({gender: 'm', $or: [{nationality: 'english'}]});
it returned.
> db.nettuts.find({gender: "m", $or: [{nationality: "english"}]});
{ "_id" : ObjectId("53064b7979d90b140e53df3e"), "first" : "michael", "last" : "caine", "dob" : "14/03/1933", "gender" : "m", "hair_colour" : "brown", "occupation" : "actor", "nationality" : "english" }
>
When I tried the same command, this time with "and" clause
db.nettuts.find({gender: 'm', $and: [{nationality: 'english'}]});
I got the same output as when I used $or
> db.nettuts.find({gender: "m", $and: [{nationality: "english"}]});
{ "_id" : ObjectId("53064b7979d90b140e53df3e"), "first" : "michael", "last" : "caine", "dob" : "14/03/1933", "gender" : "m", "hair_colour" : "brown", "occupation" : "actor", "nationality" : "english" }
>
So given these two output what is the difference between "or" clause and "and" clause here. Please help me understand.
Upvotes: 0
Views: 426
Reputation: 69663
Using $or
or $and
with an array with only one entry is quite pointless, because both operators apply to the array which is assigned to them. They don't apply to any other parts of the match-object.
When you want to find all actors which are male or english, you would do this:
db.nettus.find({
$or: [
{ "gender": "m"},
{ "nationality": "english"}
]
});
To find all actors which are male and english, you don't even need the $and-operator:
db.nettus.find({
"gender": "m",
"nationality": "english"
});
There are quite few situations where you really must use the $and-operator. The only situation where you have no alternative is when you need to apply the same operator to the same field multiple times. In this example we select all people whose age can be divided by 3 and 5.
db.people.find({
$and: [
{ age: $mod [ 3, 0] },
{ age: $mod [ 5, 0] }
]
});
Upvotes: 3