MongoDB (9)-a simple introduction to find() for document query operations

find()

  • Use find() to query documents in MongoDB
  • The find() method displays the document to be queried in an unstructured way

Grammatical format

db.collection.find(query, projection)
  • query: optional, set the query operator to specify the query conditions
  • projection: optional, specify the fields to be returned in the document matching the query, if this option is omitted, all fields are returned

pretty()

In order to view the format of the document more intuitively and beautifully, you can add a pretty() method at the end

db.inventory.find().pretty(){    "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"),    "item" : "journal",    "qty" : 25,    "size" : {        "h" : 14,        "w" : 21,        "uom" : "cm"    },    "status" : "A"}

Note: findOne() does not have a pretty() method

findOne()

And find() are query documents, but only the first document that matches the query condition successfully is returned

Grammatical format

db.collection.findOne(query, projection)

Query conditions

MongoDB supports query condition operators. The following table shows the comparison of common query condition operators between MongoDB and RDBMS (Relational Database, Mysql)

OperatorformatInstanceCompared with RDBMS where statement
Equal to (=){<key>: {<value>}}db.test.find( {price: 24})where price = 24
Greater than (>){<key>: {$gt: <value>}}db.test.find( {price: {$gt: 24}})where price> 24
Less than (<){<key>: {$lt: <value>}}db.test.find( {price: {$lt: 24}})where price <24
Greater than or equal (>=){<key>: {$gte: <value>}}db.test.find( {price: {$gte: 24}})where price >= 24
Less than or equal to (<=){<key>: {$lte: <value>}}db.test.find( {price: {$lte: 24}})where price <= 24
Not equal (!=){<key>: {$ne: <value>}}db.test.find( {price: {$ne: 24}})where price != 24
And{key01: value01, key02: value02, ...}db.test.find( {name: "Little Pineapple Test Notes", price: 24})where name = "Little Pineapple Test Notes" and price = 24
Or (or){$or: [{key01: value01}, {key02: value02}, ...]}db.test.find( {$or:[{name: "Little Pineapple Test Notes"},{price: 24}]7})where name = "Small pineapple test notes" or price = 24
in{<key>: {$in: [a,b,c,d.....]}}

db.test.find( {qty: {$in: [5, 15]}})

where qty in (5,15)
not in{<key>: {$nin: [a,b,c,d.....]}}db.test.find( {qty: {$nin: [5, 15]}})where qty not in (5,15)

Insert test data

db.inventory.insertMany([   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }]);

All chestnuts in the following are based on these data

Query all documents

> db.inventory.find( {} ){ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9e"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

Equivalent to the writing of Mysql

SELECT * FROM inventory

Query document, = equal to operation

Grammatical format

{ <field1>: <value1>}

Actual chestnuts

> db.inventory.find( { status: "D" } ){ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }

Equivalent to the writing of Mysql

SELECT * FROM inventory WHERE status = "D"

Query operator

There is a concept called query operator, which is actually the chestnuts in the query condition column above

Use the syntax format of the query operator

{ <field1>: { <operator1>: <value1> }, ... }

What other query operators will be explained in detail later

Query documents, and operations

> db.inventory.find( { status: "A", qty: { $lt: 30 } } ){ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }

Equivalent to the writing of Mysql

SELECT * FROM inventory WHERE status = "A" AND qty < 30

Query document, in operation

> db.inventory.find( { status: { $in: [ "A", "D" ] } } ){ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9e"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

Equivalent to the writing of Mysql

SELECT * FROM inventory WHERE status in ("A", "D")

Query document, or or operation

> db.inventory.find({$or:[{status:"A"},{qty:{$gt:50}}]}){ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9e"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

Equivalent to the writing of Mysql

SELECT * FROM inventory WHERE status = "A" OR qty > 50

Query document, and add or operation

Query all documents in the document selection collection whose status is "A", qty is less than ($lt) 30, or whose item starts with the character p

db.inventory.find( {     status: "A",     $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]} )

MongoDB supports regular expressions

Equivalent to the writing of Mysql

SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")