MongoDB query document usage
find()
Method.
The syntax format of MongoDB query data is as follows: query Optional, use the query operator to specify query conditions projection Optionally, use the projection operator to specify the returned key When querying, all the key values in the document are returned, simply omitting the parameter (default omitted). If you need to read data in an easy-to-read way, you can use For the following example, we queried the data in the collection col: Except If you are familiar with regular SQL data, you can better understand MongoDB conditional queries through the following table: Operation Format Example Similar statements in RDBMS Equal to {< key >: < value >} Db.col.find ({“by”: “Rookie tutorial”}) .pretty () Where by = ‘rookie tutorial’ Less than {< key >: {$lt: < value >}} Db.col.find ({“likes”: {$lt:50}}) .pretty () Where likes < 50 Less than or equal to {< key >: {$lte: < value >}} Db.col.find ({“likes”: {$lte:50}}) .pretty () Where likes < = 50 Greater than {< key >: {$gt: < value >}} Db.col.find ({“likes”: {$gt:50}}) .pretty () Where likes > 50 Greater than or equal to {< key >: {$gte: < value >}} Db.col.find ({“likes”: {$gte:50}}) .pretty () Where likes > = 50 Not equal to {< key >: {$ne: < value >}} Db.col.find ({“likes”: {$ne:50}}) .pretty () Where likes! = 50 MongoDB’s The syntax format is as follows: The following example is passed through the by And title Key to query 菜鸟教程 Medium MongoDB 教程 Data of In the above example, it is similar to The MongoDB OR conditional statement uses keywords $or The syntax format is as follows: In the following example, we demonstrate the query key by Documents with values of rookie tutorials or key title values of MongoDB tutorials. The following example demonstrates the combined use of AND and OR, similar to the regular SQL statement: ‘where likes > 50 AND (by =’ rookie tutorial’OR title = ‘MongoDB tutorial’)’
find()
Method to display all documents in an unstructured way. 3.16.1. Grammar ¶
db.collection.find(query, projection)
pretty()
Method, the syntax format is as follows:>db.col.find().pretty()
pretty()
Method to display all documents in a formatted manner. 3.16.2. Example ¶
> db.col.find().pretty()
{
"_id" : ObjectId("56063f17ade2f21f36b03133"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "菜鸟教程",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
find()
In addition to the method, there is another one.
findOne()
Method, which returns only one document.Comparison of MongoDB and RDBMS Where statements ¶
MongoDB AND condition ¶
find()
A method can pass in multiple keys (key), each key separated by a comma, that is, the AND condition of a regular SQL.>db.col.find({key1:value1, key2:value2}).pretty()
3.16.3. Example ¶
> db.col.find({"by":"菜鸟教程", "title":"MongoDB 教程"}).pretty()
{
"_id" : ObjectId("56063f17ade2f21f36b03133"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "菜鸟教程",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
WHERE
Statement: WHERE by=’菜鸟教程’ AND title=’MongoDB 教程’ MongoDB OR condition ¶
>db.col.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
3.16.4. Example ¶
>db.col.find({$or:[{"by":"菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty()
{
"_id" : ObjectId("56063f17ade2f21f36b03133"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "菜鸟教程",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>
Combined use of AND and OR ¶
>db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty()
{
"_id" : ObjectId("56063f17ade2f21f36b03133"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "菜鸟教程",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}