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

MongoDB basic Operation 1 add, delete, modify and check articles

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

Share

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

The logic of mongoDB logical memory structure MongoDB only consists of three parts: document, collection, database (database). Among them, the document is the core concept of MongoDB, which is the smallest unit of MongoDB logical storage equivalent to a row of records in a relational database, multiple documents form a collection, and the set is equivalent to the concept of tables in a relational database. Multi-combination of database SQl terminology MongoDB terminology / concept interpretation databasedatabase database tablecollection database tables / collections rowdocument data record rows / documents colummfield numeric fields / domain indexindex index table joinsindex table link MongoDB does not support primary key primary key MongoDB automatically sets-- id field as a primary key database multiple databases can be established in a MongoDB, and the default library of MongoDB is test. The database is stored in the data directory. To display it, you need to insert some data into the database. A point instance of MongoDB can accommodate multiple independent databases, each with its own set of permissions, and different databases are also placed in different files. The database retains a special database admin: from a permission point of view, this is a root database. If you add a user to this database, the user will automatically inherit the permissions of all databases. Some specific server terminal commands can only be run from this database, such as shutting down. Local: this database will never be replicated and can be used to store any collection of local single servers config: when Mongo is used for sharding settings, the config database is used internally to store the sharding-related information collection, that is, the MongonDB document group, similar to the table collection in the relational database management system (Relational DatabaseManagement), which exists in the database and has no fixed structure. This means that different formats and types of data can be inserted in the union, but usually the data inserted into the collection will have some relevance. When the first document is inserted, the collection is automatically created. MongoDB login and exit # main configuration file corresponding to starting service mongod-f # main configuration file corresponding to stopping service mongod-f-shutdown# local login (default instance port number is:-- port=27017 Do not write) > mongo# login remote host instance > mongo-- host 192.168.10.5-- port = 270 quit MongoDB > exit collection create info collection > db.createcollection ('ccc') # View collection method 1: > show tabels method 2: > show colletctions# display info collection operation command > db.info.help () document addition, deletion, modification and search # insert a record > db.info.insert ({"id": 1, "name": "zzz") "address": "Airport", "hobby": ["game", "talk", "sport"]}) # insert a piece of document data into the specified collection > db.collection.insertOne () # insert multiple document data into the specified collection > db.collection.insertMany () # insert data in batches through loops > for (var item1) I db.info.remove ({"id": "1"}) modify # modify the name value of the info collection id=1 to "zhangsan" document db.info.update ({"id": "1"} {$set: {"name": "zhangsan"}) query # query info collection all documents > db.info.find () # query info collection documents with id 1 > db.info.findOne ({id:1}) # Statistical records > db.info.count () replication database > show dbs > db.copyDatabase ("school" "school_1") / / copy a database clone collection with the same content # instance with login port number 27018 > mongo-- port 270 query database > show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB# clone info table of school database with port number 27017 to this instance database > db.runCommand ({"cloneCollection": "ccc.info") "from": "192.168.100.5 root:123123 27017"}) user authentication # login mongodbmongo user authorization (authentication login) # create a new user in the admin database root:123123 > use admin > db.createUser ({"user": "root", "pwd": "123123", "roles": ["root"]}) Successfully added user: {"user": "root" "roles": ["root"]} # exit > exit# closes mongodb service mongod-f / data/conf/mongodb1.conf-- shutdown# starts mongodb service with authentication parameters mongod-f / data/conf/mongodb1.conf-- auth# login mongodb database mongo# query database show dbs > does not display content Authentication must be authorized before the operation can be performed > use admin# uses authorized root user authentication > db.auth ("root": "123123") # query again, you can already query data > show dbsadmin 0.000GBconfig 0.000GBccc 0.000GBschool 0.000GB backup and recovery database export import data through mongoexport and mongoimport directories The format of the exported data file is JSON format or CSV format

Parameter description: d: name of database c:collection f: which columns to export o: filename to export Q: filter condition to export data # backup local school database > [root@localhost ~] # mkdir / backup/ / create backup directory [root@localhost ~] # mongodump-d ccc-o / backup/ Export # restore local school database to database abc > [root@localhost ~] # mongorestore-d abc-- dir=/backup/school# exports native school database info collection > [root@localhost ~] # mongoexport-d school-c info-o / backup/info.json# imports backup data into native school database user collection > [root@localhost ~] # mongoimport-d school-c user-file / backup/info.json# exports native school database user1 collection id=10 data > [root@localhost ~] # mongoexport-d school-c user- Q'{"id": {"$lt": 10}}'- o / backup/top10.json

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