In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Vps2 "> https://docs.mongodb.com/manual/reference/method/db.collection.find/
Query document
Db.collection.find (query, projection)
Methods:
Db.collection.find ()
Additional Methods
Db.collection.findOne
In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries.
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"}
])
Reference:
> db.inventory.find ({}, {_ id:0})
{"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"}
The operation must be in the form of key-value pairs and must be enclosed in {}, so all queries must be enclosed in {} first.
1 、 Select All Documents in a Collection
Db.inventory.find ({}) = db.inventory.find ()
2 、 Specify Equality Condition
Db.inventory.find ({status: "D"})
SELECT * FROM inventory WHERE status = "D"
{"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"}
3 、 Specify Conditions Using Query Operators
Db.inventory.find ({status: {$in: ["A", "D"]}})
SELECT * FROM inventory WHERE status in ("A", "D")
{"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"}
4 、 Specify AND Conditions
Db.inventory.find ({status: "A", qty: {$lt: 30}})
SELECT * FROM inventory WHERE status = "A" AND qty
< 30 { "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" } 5、Specify OR Conditions db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } ) SELECT * FROM inventory WHERE status = "A" OR qty < 30 { "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" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" } 6、Specify AND as well as OR Conditions db.inventory.find( { status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] } ) SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%") { "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" } Additional Query Tutorials 1、Query on Embedded/Nested Documents 查询嵌入/嵌套文档(字段值是一个文档) 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" } ]) >Db.inventory.find ({}, {_ id:0})
{"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"}
Match an Embedded/Nested Document
The value of the query size field is all documents for {hburex 14 ~ (nd) ~ (21) Magi Uom: "cm"}: (specify equality conditions on fields that are embedded / nested documents)
{"item": "journal", "qty": 25, "size": {"h": 14, "w": 21, "uom": "cm"}, "status": "A"}
Db.inventory.find ({size: {wcm 21 ~ (st) HVR 14 ~ (th) Uom: "cm"}})
Note: exact matching of documents, including order.
Db.inventory.find ({size: {wcm 21 ~ (st) HVR 14 ~ (th) uom: "cm"}) / / does not match any documents in the collection
Query on Nested Field
Specify Equality Match on a Nested Field
Query all documents where the value of the field uom nested in the size field is equal to "in":
Db.inventory.find ({"size.uom": "in"})
{"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"}
Specify Match using Query Operator
Query all documents whose value of the nested field h in the size field is less than 15:
Db.inventory.find ({"size.h": {$lt: 15}})
{"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": "postcard", "qty": 45, "size": {"h": 10, "w": 15.25 "uom": "cm"}, "status": "A"}
Specify AND Condition
Query all documents where the value of the nested field h in the size field is less than 15 and the value of in is "in" and the value of status is "D":
Db.inventory.find ({"size.h": {$lt: 15}, "size.uom": "in", status: "D"})
{"item": "paper", "qty": 100, "size": {"h": 8.5, "w": 11, "uom": "in"}, "status": "D"}
2. Query an Array query array (field value is an array)
Db.inventory.insertMany ([
{item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [14,21]}
{item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [14,21]}
{item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [14,21]}
{item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [22.85,30]}
{item: "postcard", qty: 45, tags: ["blue"], dim_cm: [10,15.25]}
])
Reference:
{"item": "journal", "qty": 25, "tags": ["blank", "red"], "dim_cm": [14,21]} {"item": "notebook", "qty": 50, "tags": ["red", "blank"], "dim_cm": [14,21]} {"item": "paper", "qty": 100 "tags": ["red", "blank", "plain"], "dim_cm": [14,21]} {"item": "planner", "qty": 75, "tags": ["blank", "red"], "dim_cm": [22.85,30]} {"item": "postcard", "qty": 45 "tags": ["blue"], "dim_cm": [10,15.25]}
Match an Array
Query all documents that have exactly two elements "red and blank" in the same order in the array field tags:
Db.inventory.find ({tags: ["red", "blank"]})
Note: exact matches, including the order of elements in the array.
{"item": "notebook", "qty": 50, "tags": ["red", "blank"], "dim_cm": [14,21]}
Query all documents in the array field tags that contain "red" and "blank" elements, regardless of the order and other elements in the array:
Db.inventory.find ({tags: {$all: ["red", "blank"]}})
Equivalent to
Db.inventory.find ({$and: [{tags: "red"}, {tags: "blank"}]}, {_ id:0})
{"item": "journal", "qty": 25, "tags": ["blank", "red"], "dim_cm": [14,21]} {"item": "notebook", "qty": 50, "tags": ["red", "blank"], "dim_cm": [14,21]} {"item": "paper", "qty": 100 "tags": ["red", "blank", "plain"], "dim_cm": [14,21]} {"item": "planner", "qty": 75, "tags": ["blank", "red"], "dim_cm": [22.85,30]}
Query an Array for an Element
Query all documents that contain the red element in the array field tags:
Db.inventory.find ({tags: "red"})
{"item": "journal", "qty": 25, "tags": ["blank", "red"], "dim_cm": [14,21]} {"item": "notebook", "qty": 50, "tags": ["red", "blank"], "dim_cm": [14,21]} {"item": "paper", "qty": 100 "tags": ["red", "blank", "plain"], "dim_cm": [14,21]} {"item": "planner", "qty": 75, "tags": ["blank", "red"], "dim_cm": [22.85,30]}
Query all documents that have at least one element in the array field dim_cm whose value is greater than 25:
Db.inventory.find ({dim_cm: {$gt: 25}})
{"item": "planner", "qty": 75, "tags": ["blank", "red"], "dim_cm": [22.85,30]}
Specify Multiple Conditions for Array Elements
Query an Array with Compound Filter Conditions on the Array Elements
Query all documents in the array field dim_cn where the value of one element is greater than 15 and the value of another element is less than 20, or that a single element meets two conditions:
Db.inventory.find ({dim_cm: {$gt: 15, $lt: 20}})
{"item": "journal", "qty": 25, "tags": ["blank", "red"], "dim_cm": [14,21]} {"item": "notebook", "qty": 50, "tags": ["red", "blank"], "dim_cm": [14,21]} {"item": "paper", "qty": 100 "tags": ["red", "blank", "plain"], "dim_cm": [14,21]} {"item": "postcard", "qty": 45, "tags": ["blue"], "dim_cm": [10,15.25]}
Query for an Array Element that Meets Multiple Criteria queries array elements that meet multiple criteria
Query all documents that have at least one element in the array field dim_cm whose value is greater than 22 and less than 30:
Db.inventory.find ({dim_cm: {$elemMatch: {$gt: 22, $lt: 30})
{"item": "planner", "qty": 75, "tags": ["blank", "red"], "dim_cm": [22.85,30]}
Query for an Element by the Array Index Position queries elements through array index location
Query all documents whose value of the second element in the array field dim_cm is greater than 25:
Db.inventory.find ({"dim_cm.1": {$gt: 25}})
{"item": "planner", "qty": 75, "tags": ["blank", "red"], "dim_cm": [22.85,30]}
Query an Array by Array Length
Query all documents with three elements in the array field tags:
Db.inventory.find ({"tags": {$size: 3}})
{"item": "paper", "qty": 100,100, "tags": ["red", "blank", "plain"], "dim_cm": [14,21]}
3. Query an Array of Embedded Documents queries the array of embedded documents (the field value contains the array, and the documents are nested in the array)
Db.inventory.insertMany ([
{item: "journal", instock: [{warehouse: "A", qty: 5}, {warehouse: "C", qty: 15}]}
{item: "notebook", instock: [{warehouse: "C", qty: 5}]}
{item: "paper", instock: [{warehouse: "A", qty: 60}, {warehouse: "B", qty: 15}]}
{item: "planner", instock: [{warehouse: "A", qty: 40}, {warehouse: "B", qty: 5}]}
{item: "postcard", instock: [{warehouse: "B", qty: 15}, {warehouse: "C", qty: 35}]}
])
Reference:
{"item": "journal", "instock": [{"warehouse": "A", "qty": 5}, {"warehouse": "C", "qty": 15}]} {"item": "notebook", "instock": [{"warehouse": "C", "qty": 5}]} {"item": "paper" "instock": [{"warehouse": "A", "qty": 60}, {"warehouse": "B", "qty": 15}]} {"item": "planner", "instock": [{"warehouse": "A", "qty": 40}, {"warehouse": "B" "qty": 5}]} {"item": "postcard", "instock": [{"warehouse": "B", "qty": 15}, {"warehouse": "C", "qty": 35}]}
Query for a Document Nested in an Array queries documents nested in an array
Query the embedded document in the array field instock for all documents that match the specified document:
Db.inventory.find ({"instock": {warehouse: "A", qty: 5}})
{"item": "journal", "instock": [{"warehouse": "A", "qty": 5}, {"warehouse": "C", "qty": 15}]}
Note: exact match, including the order of fields.
Db.inventory.find ({"instock": {qty: 5, warehouse: "A"}}) / / does not match any documents in the collection.
Specify a Query Condition on a Field in an Array of Documents
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 271
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.