Common MongoDB commands
MongoDB is a distributed database based on the database, written by C + +, between the relational database and non-relational database between the products, so in many business can replace mysql, to provide higher performance and better scalability. Although MongoDB does not support sql syntax, but from the commonly used command and sql usage is similar.
What are the databases for the query?
show dbs
Switch the database
use <db_name>
If the database does not exist, create the database, otherwise switch to the specified database. But only in the database to insert data, will be displayed in the query database list.
Queries all the collections of the current database
db.getCollectionNames()
View the status information for the specified collection
db.<collection_name>.stats()
View all the indexes in the current database
db.system.indexes.find()
View the index in the specified collection
db.col.getIndexes()
Creates an index in the specified collection
db.col.ensureIndex({“name”: 1, “age”: -1})
Index by name and age, 1 for ascending order, and -1 for descending order.
View the documents in the collection
Query all documents
db.col.find()
Display the contents of the document in an easy-to-read manner
db.col.find().pretty()
Query the top 10 documents
db.col.find().limit(10)
Sort the results of the query
db.col.find().sort({‘clock’:-1})
The query results are sorted by clock, 1 is in ascending order, -1 is in descending order.
Where condition
operating | example | Similar statements in RDBMS |
---|---|---|
equal | Db.col.find ({“key”: “value”}). Pretty () | Where key = ‘value’ |
Less than | Db.col.find ({“key”: {$ lt; 50}}). Pretty () | Where key <50 |
Less than or equal to | Db.col.find ({“key”: {$ lte: 50}}). Pretty () | Where key <= 50 |
more than the | Db.col.find ({“key”: {$ gt; 50}}). Pretty () | Where key> 50 |
greater than or equal to | Db.col.find ({“key”: {$ gte: 50}}). Pretty () | Where key> = 50 |
not equal to | Db.col.find ({“key”: {$ ne: 50}}). Pretty () | Where key! = 50 |
And condition
The find() method can pass multiple keys, each separated by commas.
db.col.find({key1:value1, key2:value2}).pretty()
Or condition
E.g db.col.find({$or:[{“key1″:”value1”}, {“key2”: “value2”}]}).pretty()
Update operation
db.collection.update( query, update, upsert, multi )
Query : update query conditions, similar to sql update operation where clause.
Update : update the object and some of the updated operators (such as $set, $inc …), etc., can also be understood as sql update operation set clause.
Upsert : optional, this parameter means that if there is no record of the update, whether to insert a new record, true for the insert, the default is false, do not insert.
Multi : optional, the default is false, only to find the first record found, if this parameter is true, put the conditions to check out all the records of all updates.
db.agent_conf.update({hostid:137}, {$set: {upload:{interval:10}}}, true)
The path of the hostid = 137 in the agent_conf collection changes the value of upload.interval to 3, and if it does not exist, it only modifies the first entry of the match.
Delete operation
db.col.remove({id:1})