preface
I have been used to using Mysql database before, but I am not familiar with the command line of MongoDb. Here is a summary of the command line of MongoDb, which can be easily referred to in the future.
Basic Usage of MongoDb
1. Perform database operations
Viewing a Database
show dbs Copy the code
Statistics database information
use test Switch to the test databaseDb.stats () {db.stats();"db" : "test"// The database name"collections": 0, // Number of sets"views": 0."objects": 0, // Number of documents"avgObjSize": 0, // Average size of each document"dataSize": 0, // Size of space occupied by data, excluding index, in bytes"storageSize": 0, // Allocated storage space"nuinExtents": 0, // Continuously allocated data blocks"indexes": 0, // Number of indexes"indexsize": 0, // Index size"fileSize": 0, // The size of the physical storage file"ok": 1}Copy the code
Deleting a Database
Db.dropdatabase () // Drop the current database, db points to the current usetestThe databaseCopy the code
View the collection under the database
db.getCollectionNames()Copy the code
2. Set operations
Create a collection
Db.createcollection (name, options) eg: db.createcollection ("yingSet", {capped:true,size:6142800, max :10000 }) Create the yingSet databaseCopy the code
parameter | type | describe |
---|---|---|
capped | Boolean | (Optional) If true, closed collections are enabled. An upper bound collection is a fixed-size collection that automatically overwrites its oldest entries when it reaches its maximum. If true is specified, the size argument also needs to be specified |
size | digital | (Optional) Specifies the maximum size, in bytes, of the upper bound collection. If capped is true, you also need to specify the value of the subfield |
max | digital | (Optional) Specifies the maximum number of documents allowed in the upper bound collection |
If you insert a document into a collection that has not been created, the collection is created first
db.yingSet.insert( {"name": "tom"})# db.yingset points to the collection objectCopy the code
View the collection under the database
show collections
cms_config
cms_page
cms_site
cms_site_server
cms_template
filesystem
fs.chunks
fs.files
sys_dictionary
user_test
Copy the code
Renaming collection
db.yingSet.renameCollection( "myset")Copy the code
Delete the collection
db.yingSet.drop()Copy the code
3. Document operations
The insert
Inserts documents without specifying the _ID field
db.test.insert( { item : "card", qty : 15 }) Insert data into the test collectionCopy the code
Inserts a document specifying the _id field, and the value _id must be unique in the collection to avoid duplicate key errors
db.test.insert(
{ _id: 10, item: "box", qty: 20 }
)
Copy the code
Insert the document as a variable
do = ({ name: "C", price: 40 })
db.test.insert(do)Copy the code
MongoDB is added in update 3.2
db.test.insertOne( { item: "card", qty: 15 } );Copy the code
Multiple documents inserted
db.test.insertMany([
{ item: "card", qty: 15 },
{ item: "envelope", qty: 20 },
{ item: "stamps", qty:30 }
])Copy the code
Update and modify operation
Obj represents the object to be updated. If a record with the same “_ID” as obj already exists in the set, Mongodb will replace the obj object with the existing record in the set. If not, the obj object is inserted.
Db.collection. save(obj) eg: db.products.save({_id: 100, item:"watern, qty: 30 })Copy the code
Delete operation
db.test.remove({'title': 'MongoDB'})Copy the code
db.collection.deleteMany ({})
db.collection.deleteMany ({ status : "A" })
db.collection.delete.One ({ status : "D" })Copy the code
Query operation
Basic condition query
db.test.find()
db.test.find().pretty()
Copy the code
The operator | format | The instance | Compare with the RDBMS WHERE statement |
---|---|---|---|
Is equal to theta = | {<key> : {<value>}} | db.test.find( {price : 24} ) | where price = 24 |
Greater than (>) | {<key> : {$gt : <value>}} | db.test.find( {price : {$gt : 24}} ) | where price > 24 |
Less than (<) | {<key> : {$lt : <value>}} | db.test.find( {price : {$lt : 24}} ) | where price < 24 |
Greater than or equal to (>= | {<key> : {$gte : <value>}} | db.test.find( {price : {$gte : 24}} ) | where price >= 24 |
Less than or equal to | {<key> : {$lte : <value>}} | db.test.find( {price : {$lte : 24}} ) | where price <= 24 |
Does not equal (! =) | {<key> : {$ne : <value>}} | db.test.find( {price : {$ne : 24}} ) | where price ! = 24 |
And (and) | {key01 : value01, key02 : value02, … } | Db.test. find({name: “”, price: 24}) | Where name = “” and price = 24 |
And (or) | {$or : [{key01 : value01}, {key02 : value02}, …] } | The db. The test. The find ({$or: [{name: “” “”}, {price: 24}]}) | Where name = “” or price = 24 |
Query fields whose age is NULL
db.test.find({age:null})Copy the code
Limit the number of query results
db.test.find().limit(3)Copy the code
Sort the query results in ascending order (1) and descending order (-1)
db.test.find().sort({"price" : 1})Copy the code
Use the $regex operator to set the regular expression that matches the string
db.test.find({tags:{$regex:"MongoDB"}})Copy the code