In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the MongoDB addition, deletion, modification and query query how to achieve, the article introduces in great detail, has a certain reference value, interested friends must read it!
Elaborate on 1. Simple query: > db.t1.find () {"_ id": ObjectId ("585ce007d993c80e8713c7bd"), "x": 1, "j": 1} {"_ id": ObjectId ("585ce007d993c80e8713c7be"), "x": 4, "j": 2} {"_ id": ObjectId ("585ce007d993c80e8713c7bf"), "x": 2, "j": 3} {"_ id": ObjectId ("585ce008d993c80e8713c7c0"), "x": 4 "j": 4} {"_ id": ObjectId ("585ce023d993c80e8713c7c2"), "x": 3} 2. Sort query: > db.t1.find () .sort ({jjjjjjlur 1})-"1" indicates ascending order "- 1" means descending {"_ id": ObjectId ("585ce008d993c80e8713c7c0"), "x": 4, "j": 4} {"_ id": ObjectId ("585ce007d993c80e8713c7bf"), "x": 2, "j": 3} {"_ id": ObjectId ("585ce007d993c80e8713c7be"), "x": 4, "j": 2} {"_ id": ObjectId ("585ce007d993c80e8713c7bd"), "x": 1 "j": 1} {"_ id": ObjectId ("585ce023d993c80e8713c7c2"), "x": 3} 3. Conditional query: > db.t1.find ({x id 2}) {"_ id": ObjectId ("585ce007d993c80e8713c7bf"), "x": 2, "j": 3} 4. Multi-conditional query > db.t1.find ({x _ id: ObjectId ("585ce007d993c80e8713c7bf"), "x": 2, "j": 3} 5. Comparison condition query > db.t1.find ({j: {$gt:2,$lte:4}})-query records with j fields greater than 2 and less than or equal to 4 {"_ id": ObjectId ("585ce007d993c80e8713c7bf"), "x": 2, "j": 3} {"_ id": ObjectId ("585ce008d993c80e8713c7c0"), "x": 4, "j": 4}
Comparison operator:
$gt: greater than
$lt: less than
$gte: greater than or equal to
$lte: less than or equal to
$ne: not equal to
6. Multiple values in query (similar to IN) > db.t1.find ({j: {$in: [1JZ 4]}})-records of jig1 or jig4, excluding records that can be used with $nin {"_ id": ObjectId ("585ce007d993c80e8713c7bd"), "x": 1, "j": 1} {"_ id": ObjectId ("585ce008d993c80e8713c7c0"), "x": 4, "j": 4} 7. Multiple-value and query (similar to AND) > db.t1.find ({xjan1dagj: {$gt:1}})-Xint1 and j > 1 conditional record {"_ id": ObjectId ("585ce007d993c80e8713c7be"), "x": 1, "j": 2} {"_ id": ObjectId ("585ce007d993c80e8713c7bf"), "x": 1, "j": 3} 8. Multiple-value or query (similar to OR) > db.t1.find ({$or: [{XRV 4}, {j: {$lte:2}]})-XRV 4 or j db.t1.find ({XRV 1 and and (jdb.t6.find ({j: {$exists:false}))-query the words containing j fields This is $exists:true {"_ id": ObjectId ("585ce023d993c80e8713c7c2"), "x": 3} 11. Return only a field or some fields > db.t1.find ({Xid 4}, {jlv 1})-all field values are displayed by default. The second curly braces use JVR 1 to indicate that only j fields are returned, but the primary key _ id displays {"_ id": ObjectId ("585ce008d993c80e8713c7c0"), "j": 4} {"_ id": ObjectId ("585ce007d993c80e8713c7be"), "j": 2} 12 by default. How do you want to eliminate the return of primary key _ id > db.t1.find ({XRU 4}, {JRV 1cmperid 0}) {"j": 4} {"j": 2} nested subdocument queries
Original data Table:
> db.t6.find () {"_ id": 1, "x": 2, "kk": {"deviceID": 222, "city": "Tianjin"}} {"_ id": 2, "x": 3, "kk": {"deviceID": 333, "city": "Beijing"} {"_ id": 3, "x": 2 "kk": {"deviceID": 234}}
Query fields in nested subdocuments. You can use points to connect external fields and embedded fields. You may not quite understand what it means. It doesn't matter. Just look at the following example.
13. Make a matching query based on the values of certain fields in the subdocument: > db.t6.find ({kk: {deviceID:222,city: "Tianjin"})-query records {"_ id": 1, "x": 2, "kk": {"deviceID": 222, "city": "Tianjin"}} for deviceID=222 and city: "Tianjin" in the KK subdocument.
Or use the following form
> db.t6.find ({"kk.deviceID": 222, "kk.city": "Tianjin"})-- same effect as the previous method. Note the use of double quotes {"_ id": 1, "x": 2, "kk": {"deviceID": 222, "city": "Tianjin"} 14. Query data from city fields that do not exist in nested documents
> db.getCollection ('t6'). Find ({"kk.city": {$exists:false}}) {"_ id": 3, "x": 2, "kk": {"deviceID": 234}} 15. The query result only wants to return some fields in the subdocument with values > db.t6.find ({}, {"kk.deviceID": 1) {"kk": {"deviceID": 222}} {"kk": {"deviceID": 333}} {"kk": {"deviceID": 234}}.
Another: of course, there are more and more complex query statements about nested subdocuments, mainly depending on the data structure in your nested documents. The more complex the structure, the more complex the query statements you need to write.
Array query
Querying arrays in MongoDB is also cumbersome because, after all, other relational databases do not contain this data type. But it doesn't matter. I'll try my best to list the scenes I can think of here for you and me to find.
Original data Table:
> db.t5.find () {"_ id": 1, "x": 2, "Course": ["English", "Math"], "score": [1,3]} {"_ id": 2, "x": 3, "Course": ["Math", "English"], "score": [18,12]} {"_ id": 3, "x": 4 "Course": ["English"], "score": [98,1]} {"_ id": 4, "x": 5, "Course": ["Yuwen", "English"], "score": [45,46]} {"_ id": 5, "x": 7, "Course": ["Math"], "score": [99,100]} 16. Query the elements in the array precisely
Requirement: use {:} form
Example 1: match an element
> db.t5.find ({Course: "Math"})-all lines {"_ id": 1, "x": 2, "Course": ["English", "Math"], "score": [1,3]} {"_ id": 2, "x": 3, "Course": ["Math", "English"] are found "score": [18,12]} {"_ id": 5, "x": 7, "Course": ["Math"], "score": [99,100]}
Example 2: accurately query multiple elements
> db.t5.find ({Course: ["Math", "English"]}) {"_ id": 2, "x": 3, "Course": ["Math", "English"], "score": [18,12]}
Note: the above statement will query only one row, and the other data that also contains "Math" and "English" contains the same elements, but the order of the elements is not the same. So it's not listed.
Example 3: query an element precisely (by element location)
> db.t5.find ({"Course.1": "English"})-the second element in the exact query array is "English" {"_ id": 2, "x": 3, "Course": ["Math", "English"], "score": [18,12]} {"_ id": 4, "x": 5, "Course": ["Yuwen", "English"] Score: [45,46]} 17. Specify a multi-conditional query array
Requirement: use the $elemMatch operator
Example 1: only one element in the array is required to meet all the conditions
> db.t5.find ({score: {$elemMatch: {$gt:15,$lt:20}})-only 18 > 15, and 18 db.t5.find ({score: {$gt:15,$lt:20}}) {"_ id": 2, "x": 3, "Course": ["Math", "English"], "score": [18,12]} {"_ id": 3, "x": 4 "Course": ["English"], "score": [98,1]}
Another: for the above example, you may be confused. Why can [98jue 1] be selected? Because 115, so meet the query conditions, understand?
18. Get the array length of the array field > db.t5.find ({_ id:5}) .forEach (function (x) {var score=x.score; var scoreLenth=score.length; print (scoreLenth);}); 2-- return the result
The view array is not empty:
> db.t9.find ({arrary: {$elemMatch: {$ne:null}}) regular expression
> db.t4.find () {"_ id": ObjectId ("58aa8e4c151e4fd0af703688"), "name": "Jack", "addr": "tianjin"} {"_ id": ObjectId ("58aa8e5e151e4fd0af703689"), "name": "Tom", "addr": "beijing"} {"_ id": ObjectId ("58aa9c25151e4fd0af70368a"), "name": "Nick J"} 19. Query name records starting with J > db.t4.find ({name:/ ^ J /})-query records starting with J {"_ id": ObjectId ("58aa8e4c151e4fd0af703688"), "name": "Jack", "addr": "tianjin"} 20. Query records that do not begin with J > db.t4.find ({name: {$not:/ ^ J /}})-query records that do not begin with J {"_ id": ObjectId ("58aa8e5e151e4fd0af703689"), "name": "Tom", "addr": "beijing"} {"_ id": ObjectId ("58aa9c25151e4fd0af70368a"), "name": "Nick J"} 21. Query name re-contains J records > db.t4.find ({name:/J/})-query name re-contains J records {"_ id": ObjectId ("58aa8e4c151e4fd0af703688"), "name": "Jack", "addr": "tianjin"} {"_ id": ObjectId ("58aa9c25151e4fd0af70368a"), "name": "Nick J"} query Null or fields that do not exist
Original collection data:
> db.t8.find () {"_ id": 99, "name": null} {"_ id": 100} 22. Query the record of name:null: > db.t8.find ({name:null})-this result occurs because this query method returns name=null and does not have a record with a name field {"_ id": 99, "name": null} {"_ id": 100} 23. Query only name:null data, and do not return > db.t8.find ({name: {$type:10}}) without name fields.-you can use $type,10 to represent the data type null {"_ id": 99, "name": null} 24. Only data that does not contain name fields is returned > db.t8.find ({name: {$exists:false}})-if only records with name fields are queried, $exists:true {"_ id": 100} iterative query
The find () query returns a cursor cursor that iterates 20 times by default to display the first 20 document records by mongo shell.
If you use variables to iterate over the result set:
> var a=db.t6.find ()-- define variable > while (a.hasNext ()) {- determine the cursor position of the variable. Printjson (a.next ());-print cursor content.} {"_ id": 1, "x": 2, "kk": {"deviceID": 222, "city": "Tianjin"}} {"_ id": 2, "x": 2, "kk": {"deviceID": 222, "addr": "Heping"}
Or use forEach ()
> var a=db.t6.find () > a.forEach (printjson) {"_ id": 1, "x": 2, "kk": {"deviceID": 222, "city": "Tianjin"}} {"_ id": 2, "x": 2, "kk": {"deviceID": 222, "addr": "Heping"}
Use toArry () to query the result sets one by one
> var a=db.t6.find ()-define variables > var doc=a.toArray ()-- use toArray () to generate array result sets > doc [0]-the first document in the query result set {"_ id": 1, "x": 2, "kk": {"deviceID": 222 "city": "Tianjin"}} above are all the contents of the article entitled "how to implement the query for adding, deleting, modifying and querying MongoDB" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.