Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summarize the basic operations of MongoDB database

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the basic operation of summarizing MongoDB database, the content is clear and clear, interested friends can learn, I believe it will be helpful after reading.

This paper describes the basic operation of MongoDB database with an example. Share with you for your reference, the details are as follows:

1. Test2 cannot be found until database > use test > db.test.insert ({"name": 1}) is inserted. View the database > show dbs3. Delete database > use test > db.dropDatabase () 4. Create a collection 4.1 A collection of concepts is a set of documents, equivalent to multiple records. > db.title.insert ({"name": "hyx"}) is inserted to create the collection 5. View the collection > show collections6. Delete collection > use test > db.title.drop () 7. Insert document 7.1 document concept multiple keys and their associated values are placed together in an orderly manner. The document is similar to json data > db.file.insert ({name: "huangyuxin", age:11}) 8. View the document > db.files.find () 9. Insert document > document= ({by: "hyx"}) {"by": "hyx"} > db.file.insert (document) WriteResult ({"nInserted": 1}) > db.file.find () {"_ id": ObjectId ("5c6e8a060fc535200b893f29"), "name": "huangyuxin", "age": 11} {"_ id": ObjectId ("5c6e8b1c0fc535200b893f2a"), "by": "hyx"} > 10. Insert multiple > var res = db.file.insertMany ([{"b": 3}, {'centering: 4}]) > res {"acknowledged": true, "insertedIds": [ObjectId ("5c6e8bba0fc535200b893f2b"), ObjectId ("5c6e8bba0fc535200b893f2c")]} > db.file.find () {"_ id": ObjectId ("5c6e8a060fc535200b893f29"), "name": "huangyuxin" "age": 11} {"_ id": ObjectId ("5c6e8b1c0fc535200b893f2a"), "by": "hyx"} {"_ id": ObjectId ("5c6e8bba0fc535200b893f2b"), "b": 3} {"_ id": ObjectId ("5c6e8bba0fc535200b893f2c"), "c": 4} > 11. Update document > db.file.update ({"name": "huangyuxin"}, {$set: {"name": "hyx"}) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.file.find () {"_ id": ObjectId ("5c6e8a060fc535200b893f29"), "name": "hyx", "age": 11} {"_ id": ObjectId ("5c6e8b1c0fc535200b893f2a") "by": "hyx"} {"_ id": ObjectId ("5c6e8bba0fc535200b893f2b"), "b": 3} {"_ id": ObjectId ("5c6e8bba0fc535200b893f2c"), "c": 4} {"_ id": ObjectId ("5c6e8cdf0fc535200b893f2d"), "name": "hyx"} > db.file.save ({"_ id": ObjectId ("5c6e8b1c0fc535200b893f2a"), "name": "hyx"}) WriteResult ({"nMatched": 1) "nUpserted": 0, "nModified": 1}) > db.file.find () {"_ id": ObjectId ("5c6e8a060fc535200b893f29"), "name": "hyx", "age": 11} {"_ id": ObjectId ("5c6e8b1c0fc535200b893f2a"), "name": "hyx"} {"_ id": ObjectId ("5c6e8bba0fc535200b893f2b"), "b": 3} {"_ id": ObjectId ("5c6e8bba0fc535200b893f2c") "c": 4} {"_ id": ObjectId ("5c6e8cdf0fc535200b893f2d"), "name": "hyx"} > 12. Delete document 12.1 Delete specified document > db.title.find () {"_ id": ObjectId ("5c6e89060fc535200b893f27"), "name": "yx"} > db.file.find () {"_ id": ObjectId ("5c6e8a060fc535200b893f29"), "name": "hyx", "age": 11} {"_ id": ObjectId ("5c6e8b1c0fc535200b893f2a"), "name": "hyx"} {"_ id": ObjectId ("5c6e8bba0fc535200b893f2b") "b": 3} {"_ id": ObjectId ("5c6e8bba0fc535200b893f2c"), "c": 4} {"_ id": ObjectId ("5c6e8cdf0fc535200b893f2d"), "name": "hyx"} > db.file.remove ({"b": 3}) WriteResult ({"nRemoved": 1}) > db.file.find () {"_ id": ObjectId ("5c6e8a060fc535200b893f29"), "name": "hyx" "age": 11} {"_ id": ObjectId ("5c6e8b1c0fc535200b893f2a"), "name": "hyx"} {"_ id": ObjectId ("5c6e8bba0fc535200b893f2c"), "c": 4} {"_ id": ObjectId ("5c6e8cdf0fc535200b893f2d") "name": "hyx"} > 12.2 Delete all documents > db.file.deleteMany ({}) 12.3 Delete multiple documents > db.file.deleteMany ({status: 1}) Delete all documents with status equal to 1 in the current library 13. Conditional expression 13.1$ gt greater than query data with age greater than 0 > db.title.find ({age: {$gt: 0}}) {"_ id": ObjectId ("5c6f7d633ea8783bbfb7fd5e"), "age": 10} > 13..2$ lt less than 13.3$ gte greater than or equal to $lte less than or equal to query data with age greater than or equal to 0 > db.title.find ({age: {$gte: 1}}) 13.4 greater than less than > db.title.find ({age: {$lt:13) $gt:10}}) {"_ id": ObjectId ("5c6f7ded3ea8783bbfb7fd5f"), "age": 12} {"_ id": ObjectId ("5c6f7e833ea8783bbfb7fd60"), "age": 12} > 13.5$ ne is not equal to $eq equals 14. The $type operator $type operator retrieves matching data types in the collection based on BSON types. And return the result.

> db.title.find ({"name": {$type: 2}}) {"_ id": ObjectId ("5c6e89060fc535200b893f27"), "name": "yx"} > 15. Limit () query specified number of entries > db.title.find (). Limit (2) {"_ id": ObjectId ("5c6e89060fc535200b893f27"), "name": "yx"} {"_ id": ObjectId ("5c6f7d633ea8783bbfb7fd5e") "age": 10} > first {} put where condition Empty means to return all documents in the collection. The second {} specifies which columns are displayed and not displayed (0 means no display 1 means display). > db.title.find ({}, {"name": 1 16.skip 0}) .limit (1) {"name": "yx"} > 16.skip () skip a few pieces of data and do not easily use Skip to query, otherwise a large amount of data will lead to a sharp decline in performance, because the skip is numbered one by one, and more naturally slows down. 17.sort () 1 is in ascending order, while-1 is used in descending order. > db.title.find ({}, {'age':1,_id:0}). Sort ({age:1}) {} {"age": 10} {"age": 12} {"age": 12} > db.title.find ({}, {' age':1,_id:0}). Sort ({age:-1}) {"age": 12} {"age": 10} {} > 18. Index 18.1 create a single index 1 to create an index in ascending order for the specified The descending index is specified as-1 > db.title.createIndex ({"age": 1}) 18.2 create multiple indexes > db.title.createIndex ({"name": 1) "age":-1}) 18.3 View Index > db.col.getIndexes () 18.4 View Index size > db.col.totalIndexSize () 18.5 Delete all Collection Indexes > db.col.dropIndexes () 18.6 Delete specified Index > > db.title.dropIndex ({'age':1}) {"nIndexesWas": 2, "ok": 1} > read the above Do you have a further understanding of the basic operation of summarizing MongoDB database? if you want to learn more, you are 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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report