In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to carry out MongoDB query documents, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Query file
Use the inventory collection. To insert the contents of the inventory collection, run the following command:
Db.inventory.insertMany ([{item: "journal", qty: 25, size: {h: 14, w: 21, uom: "cm"}, status: "A"}, {item: "notebook", qty: 50, size: {h: 8.5,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"}]) Select all documents in the collection
To select all documents in the collection, pass the empty document as a query filter parameter to the find method. Query filter parameters determine selection criteria:
Db.inventory.find ({})
This action is equivalent to the following SQL statement:
SELECT * FROM inventory
For more information about method syntax, see find ().
Specify equality condition
To specify equality conditions, use expressions in query filter documents:
{
< field1 >:
< value1 >,...}
The following example selects all documents "D" that status equals from the inventory collection:
Db.inventory.find ({status: "D"})
This action corresponds to the following SQL statement:
SELECT * FROM inventory WHERE status = "D"
Specify conditions using the query operator
Query filter documents can use query operators to specify conditions in the following form:
{
< field1 >: {
< operator1 >:
< value1 >},...}
The following example retrieves all documents from the inventory collection, where status equals "A" or "D":
Db.inventory.find ({status: {$in: ["A", "D"]}}) Note
Although you can use the $or operator in a query, use the $in operator instead of the $or operator when performing an equality check on the same field.
This action corresponds to the following SQL statement:
SELECT * FROM inventory WHERE status in ("A", "D")
For a complete list of MongoDB query operators, see the Query and Projection Operators documentation.
Specify AND condition
A composite query can specify conditions for multiple fields in the document of a collection. Implicitly, a logical AND join joins the clauses of a composite query so that the query selects documents in the collection that match all criteria.
The following example retrieves all documents in the inventory collection with statusequals "A" and qty less than ($lt) 30:
Db.inventory.find ({status: "A", qty: {$lt: 30}})
This action corresponds to the following SQL statement:
SELECT * FROM inventory WHERE status = "A" AND qty < 30
See the comparison operator for other MongoDB comparison operations.
Specify OR condition
Using the $or operator, you can specify a composite query that concatenates each clause to a logical OR join so that the query selects documents in the collection that match at least one condition.
The following example retrieves all documents in the collection whose status is equal to "A" or whose qty is less than ($lt) 30:
Db.inventory.find ({status: "A", qty: {$lt: 30}})
This action corresponds to the following SQL statement:
SELECT * FROM inventory WHERE status = "A" OR qty < 30 Note
For other MongoDB comparison operators, see comparison operators.
Specify AND and OR conditions
In the following example, a composite query is made against the collection to get documents whose status is equal to "A" and qty is less than ($lt) 30 or item's value begins with p:
Db.inventory.find ({status: "A", $or: [{qty: {$lt: 30}}, {item: / ^ p /}]})
This action corresponds to the following SQL statement:
SELECT * FROM inventory WHERE status = "A" AND (qty < 30 OR item LIKE "p%") Note
MongoDB supports the regular expression $regex query to perform string pattern matching.
After reading the above, have you mastered how to query documents on MongoDB? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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: 0
*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.