Let's find all the books that have the name Oliver Twist
or are from Dover Publications
. For the sake of brevity, we shall select only the name
field as follows:
db.books.find({ $or : [ { name: "Oliver Twist"} , {publisher : "Dover Publications"} ] })
This will give us our result set of books with either the name
as Oliver Twist
or publisher
as Dover Publications
.
The previous query is similar to the following:
SELECT * FROM books WHERE publisher = "Dover Publications" OR name = "Oliver Twist";
Let's look at the query parameters in a little more detail:
{$or : [ {name: "Oliver Twist"}, {publisher : "Dover Publications"} ] }
$or
is a special operator in MongoDB and takes an array of query parameters. We can use this in conjunction with other parameters too:
db.books.find({ published_on: ISODate("2002-12-30"), $or : [ { name: "Oliver Twist"} , {publisher : "Dover Publications"} ] })
This would query with AND
and OR
. Its...