1. Select a database
Command: Use Datebase_name (database name)
Function: If the database is not created directly, if any, switch the database
The following results are performed:
Use Runoob
Switched to DB Runoob
Db
Runoob 2. View all current Databases
Command: Show DBS
The following results are performed:
Show DBS
Ad 0.000GB
Admin 0.000GB
Config 0.000GB
Local 0.000GB
MyCol 0.000GB
mycol001 0.000GB
Name 0.000GB
NB 0.000GB
1. **3. Delete database operation * *
command: db.dropdatabase ()
Execution effect:
Db.dropdatabase ()
{"Dropped": "Name", "OK": 1}
**4. Insert Document * *
command: DB. Collection_name.insert (document)
execution effect:
Db.col.insert ({name: "Lang Chi",
Age: "24",
Hobby: "Fly Ah Fly"
})
Variable document can be defined directly
Document= ({title: ' MongoDB Tutorial ',
Description: ' MongoDB is a Nosql database ',
By: ' Rookie Tutorial ',
URL: ' http://www.runoob.com ',
Tags: [' mongodb ', ' database ', ' NoSQL '],
likes:100
})
You can use the execution effect directly after you define the variable
as follows:
Db.col.insert (document)
Writeresult ({"ninserted": 1})
You can also use the Db.col.save (document) command to insert documents. If you do not specify the _id field, the Save () method is similar to the Insert () method. If you specify the _id field, the data for the _id is updated
NOTE: The following syntax is available for inserting a document after version 3.2:
Db.collection.insertOne (): Inserts a document data
into the specified collection Db.collection.insertMany (): Inserts multiple document data into the specified collection
inserting A single piece of data
var document=db.col.insertone ({"AA": 9})
Document
{
"acknowledged": true,
"Insertedid": ObjectId ("5a3761ac667e4339fe9b3a31")
}
##### inserting more than one data
>
var res = Db.collection.insertMany ([{"B": 3}, {' C ': 4}])
Res
{
"acknowledged": true,
"Insertedids": [
ObjectId ("571a22a911a82a1d94c02337"),
ObjectId ( "571a22a911a82a1d94c02338")
]
}
5. Update the existing document command: Db.collection.update (query, update, {upsert:boolean, Multi:boolean, Writeconcern:
Document}) Parameter description: The query:update query condition, similar to where in SQL update query. Update:update objects and some updated operators (such as $, $inc ...
), and so on, can also be understood as Upsert in SQL Update query after set: optional, this parameter means that if there is no record of the update, insert Objnew,true is inserted, default is False, not inserted.
Multi: Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
Writeconcern: Optional, throws an exception level. Execution effect: ' ##### #查询集合内容 > Db.col.find (). Pretty () {"_id": ObjectId ("5a37 5db0667e4339fe9b3a30 ")," title ":" MongoDB "," description ":" MongoDB is a Nosql database "," by ":" Rookie tutorial
"," url ":" Http://www.runoob.com "," tags ": [" MongoDB "," Database ", "NoSQL"], "likes": # # # # # # # # # # # # # # # # #修改某一字段 > Db.col.update ({title: "MongoDB"},{$set: {title: "Mong ODB Modify this field "}}" Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) # # # # # # # # # # # # # #再次查Consult the collection content > Db.col.find (). Pretty () {"_id": ObjectId ("5a375db0667e4339fe9b3a30"), "title": "MongoDB JJD Fdjivnjds "," description ":" MongoDB is a Nosql database "," by ":" Rookie Tutorial "," url ":" Http://www.runoob.com "," tags ": [" MongoDB "," Database "," NoSQL "]," l Ikes ": 100} at the beginning of version 3.2, MongoDB provides the following methods for updating the collection document: Db.collection.updateOne () updates a single document to the specified collection Db.collection.updateMany () updates multiple to the specified collection Document ' First we insert the test data in the set of tests >use testing >db.col.insert ([{"Name": "ABC", "Age": "+", "status": "Zxc"}, {"Name": "Dec", " Age ":" + "," status ":" Qwe "}, {" Name ":" ASD "," Age ":" + "," status ":" Nmn "},]) update a single document > Db.test_collection.updateOne ({" Name ":" abc "},{$set: {" Age ":" {"}}) {" Acknowledged ": true," Matchedcount ": 1," Modifiedcount ": 1} > Db.test_collecti On.find () {"_id": ObjectId ("59c8ba673b92ae498a5716af"), "name": "ABC", "Age": "+", "status": "Zxc"} {"_id": objec TId ("59c8ba673b92ae498a5716b0"), "NamE ":" Dec "," Age ":" + "," status ":" Qwe "} {" _id ": ObjectId (" 59c8ba673b92ae498a5716b1 ")," name ":" ASD "," Age ":" 30 ", "Status": "Nmn"} > Update multiple Documents > Db.test_collection.updateMany ({"Age": {$gt: "Ten"}},{$set: {"status": "XYZ"}}) {" Acknowledged ": true," Matchedcount ": 3," Modifiedcount ": 3} > Db.test_collection.find () {" _id ": ObjectId (" 59c8ba6 73b92ae498a5716af ")," name ":" ABC "," Age ":" + "," status ":" XYZ "} {" _id ": ObjectId (" 59c8ba673b92ae498a5716b0 ")," Nam E ":" Dec "," Age ":" + "," status ":" XYZ "} {" _id ": ObjectId (" 59c8ba673b92ae498a5716b1 ")," name ":" ASD "," Age ":" 30 ", "Status": "XYZ"} >
Writeconcern.none: No exception thrown
Writeconcern.normal: Throw only network error exception, no server error exception
Writeconcern.safe: Throws a network error exception, server error exception, and waits for the server to complete the write operation.
Writeconcern.majority: Throws a network error exception, server error exception, and waits for a master server to complete the write operation.
Writeconcern.fsync_safe: Throws network error exception, server error exception, write operation waits for server to flush data to disk.
Writeconcern.journal_safe: Throws a network error exception, a server error exception, and a write operation waits for the server to submit a log file to disk.
Writeconcern.replicas_safe: Throws a network error exception, server error exception, wait for at least 2 servers to complete the write operation. Delete a document
If you delete all the documents under the collection:
Db.colletion_name.deleteMany ({})
Delete all documents with status equal to a:
Db.colletion_name.deleteMany ({status: "A"})
Delete a document that has status equal to D:
Db.colletion_name.deleteOne ({status: "D"}). Querying documents
Command: Db.col.find (query,projection)
Query: Optional, use the query operator to specify the criteria
Projection: optional, use the projection operator to specify the returned key. Returns all the key values in the document when queried, just omit the argument (omitted by default).