In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to use NoSQL statements in MongoDB. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Check the help command
> hlep-- server level
> db.help ()-- db level
> db. Collectionname .help ()-- Collection level
View all databases
> show dbs
The newly created database is not in the database column collection. To display it, we need to create a collection to the newly created database.
View the current database
> db
Create a database
> use DATABASE_NAME
Switch to some data
> use DATABASE_NAME
If the current database is deleted, the database files on disk will be deleted as well.
> db.dropDatabase ()
Repair the database
> db.repairDatabase ()
Copy database test to test999
> db.copyDatabase ('test','test999')
View all collections (relational databases are called tables)
> show collections
> show tables
View the status of each collection
> db.printCollectionStats ()
Create a new collection
Db.createCollection ("collection name", {size attribute of the collection: size value, growth attribute of the collection: growth value, maximum capacity attribute of the collection: maximum capacity value, etc.})
> db.createCollection ("table1")
Delete Collection table1
> db.table1.drop ()
Rename the collection table1 to table101
> db.table1.renameCollection ("table101")
View the database name of the collection table1
> db.table1.getDB ()
View the status of the collection table1
> db.table1.stats ()
Query set
> db.table1.find ()-- query all data in the collection
> db.table1.findOne ()-- query the first piece of data in the collection
> db.table1.count ()-- Total number of rows
> db.table1.totalSize ()-- Total size of the collection
> db.table1.storageSize ()-- the storage space of the collection
> db.table1.distinct ("hid")-- query only column hid and list the non-repeating values of that column
> db.table1.find ({"hid": 2})-hid=2
> db.table1.find ({"hid": 2, "hid2": 3})-hid=2 and hid2=3
> db.table1.find ({$or: [{"hid": 2}, {"hid2": 3}]})-- hid=2 or hid2=3
> db.table1.find ({"hid": {$gt:1}})-- hid > 1
> db.table1.find ({"hid": {$gte:1})-- hid > = 1
> db.table1.find ({"hid": {$lt:2})-- hiddb.table1.find ({"hid": {$lte:2}})-- hiddb.table1.find ({"hid": / 2 /})-- hid like'% 2%'
> db.table1.find ({"hid": / ^ 2 /})-- hid like'2%'
> db.table1.find ({}, {"hid": 1, "go2": 1})-- query the specified two columns hid and go2
> db.table1.find ({"hid": 23}, {"hid": 1, "go2": 1})-- select hid,go2 from table1 where hid=23
> db.table1.find ({}, {"hid": true, "go2": true})-- query the specified two columns hid and go2
> db.table1.find ({"hid": 23}, {"hid": true, "go2": true})-select hid,go2 from table1 where hid=23
> db.table1.find () .sort ({"hid": 1})-- query results are sorted by hid field order
> db.table1.find () .sort ({"hid":-1})-- query results are sorted in descending order by hid field
> db.table1.find () .limit (2)-- query the first two pieces of data
> db.table1.find () .skip (2)-- query all data after Article 2
> db.table1.find () .limit (3) .skip (2)-- query the last three pieces of data after Article 2
> db.table1.find ({"hid": 2}) .count ()-- Total number of rows queried by hid=2
Insert collection (relational database is called row, mongodb is called document, no manual submission is required after insert, which can be seen by other sessions)
Method 1
Db.collectionname.insert ({Field name: "Field value"})
> db.table1.insert ({hid: "1"})
Method 2
Db. Collection name .save ({Field name: "Field value"})
> db.table1.save ({hid:2,hname: "hao2"})
Insert the table1 collection in a loop
For (var I = 0; I
< 30; i++) db.table1.save({hid: "u_" + i, age: 22 + i, sex: i % 2}); 更新集合( 关系型数据库叫行,mongodb叫文档 ) >Db.table1.update ({'hid':2}, {$set: {' hid':4}}, {multi:true})
Change the value of hid field from 2 to 4
Update modifies the first discovered row by default, and the multi:true collection shows the modification of multiple lines
Delete collection (relational database is called row, mongodb is called document)
> db.table1.remove ({'hid2':5})-- Delete the row with hid2 5
> db.table1.remove ({})-- Delete all lines
Create an index
After the index is created, there will be an extra file at the beginning of index- in the database directory
> db.table1.ensureIndex ({"hid": 1})
> db.table1.ensureIndex ({"hid": 1, "hid2":-1}, {unique:true})
1 indicates upgrade sort,-1 indicates descending sort, "hid": 1, "hid2":-1 indicates composite index, and unique:true represents unique index.
View the index information of the collection table1
> db.table1.getIndexes ()
Delete the index of the collection table1
> db.table1.dropIndexes ()
Create a user
> db.createUser ({user: "admin", pwd: "admin", roles: [{role: "userAdminAnyDatabase", db: "admin"}]})
> db.createUser ({user: "admin1", pwd: "admin1", roles: [{role: "root", db: "admin"}]})
Three methods of querying users
> use admin
> show users
> db.system.users.find ()
> db.system.users.find () .pretty ()
Query current user
Db.runCommand ({connectionStatus:1})
Create a statement for replicate. Before creating a replicate, the replSet parameter must be added when the two nodes start, and the replSet parameter value must be the same, for example, replicate1.
> use admin
> config= {_ id:'replicate1',members: [{_ id:0,host:'172.22.1.157:27017'}, {_ id:1,host:'172.22.1.158:27017'}]}
> rs.initiate (config)
> rs.status (config)
> rs.status ()
> show dbs
Then execute the following from the library
> rs.slaveOk ()
> show dbs
View replicate replication status
> db.printReplicationInfo ()
Query the shard version of the collection table1
> db.table1.getShardVersion ()
View shard sliced letters
> db.printShardingStatus ()
Start
Use the mongod command, followed by parameters
Mongod-f / mongodb/mongodb.conf
Close
Method 1.
Use admin
Db.shutdownServer ()
Method 2. (if-f is used for startup,-f should be added when it is closed)
Mongod-shutdown
Mongod-- shutdown-f / mongodb/mongodb.conf
Method 3. (do not add-9, otherwise the next startup will not start, and you must add-- repair when you need to delete the mongod.lock file or use mongod to start successfully)
Kill
Mongodb switch log
If the running time is long, the log of mongodb will be very large. You can switch, generate a new log, delete the old log, and execute this statement, which will not affect the operation of the mongodb service.
> use admin
> db.runCommand ({logRotate:1})
After execution, a new log file with the same name will be generated, and the previous log will be saved in time format.
If it is a replicate environment, it will not affect replicate,primary 's execution of this statement to switch only its own logs, does not affect the services of the secondary environment, nor will it switch the logs of the secondary environment; the execution of this statement by secondary will only switch its own logs and will not affect the services of the primary environment, nor will it switch the logs of the primary environment
The log file of mongodb will still exist after reboot, and the shutdown startup information involved in the restart will be appended to this log file.
After reading the above, do you have any further understanding of how to use NoSQL statements in MongoDB? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.