preface
This tutorial is suitable for those who have some experience in database development. For children’s shoes that are just beginning to contact the database, it is recommended to jump directly to the references of this article to choose a more suitable tutorial. Another thing to note is that there are great differences between version 2 and version 3 of mongodb. Be sure to pay attention when viewing the tutorial~
General instructions
use DATABASE_NAME
: switch to the database. If the database does not exist, it will be created automaticallyshow dbs
: view all databasesshow tables
: view all collectionsdb.dropDatabase()
: delete the database after entering the databasedb.COLLECTION_NAME.drop()
: delete collectiondb.COLLECTION_NAME.find()
: view all or specific content in the collection
Insert document
db.COLLECTION_NAME.insertOne()
: inserts a piece of document data into the specified collectiondb.COLLECTION_NAME.insertMany()
: inserts multiple document data into the specified collection
There are many equivalent insert operations
db.collection.update()
: upsert:truedb.collection.updateOne()
: upsert:truedb.collection.updateMany()
: upsert:truedb.collection.findAndModify()
: upsert:truedb.collection.findOneAndUpdate()
: upsert:truedb.collection.findOneAndReplace()
: upsert:truedb.collection.save()
.db.collection.bulkWrite()
.
Update document
update
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
Query: query criteria of update, similar to the query criteria after where in SQL update query.
Update: update object and some Update Operators (such as $, $Inc…) And so on, which can also be understood as the data after the set in the SQL update query
Upsert: optional. This parameter means whether to insert objnew if there is no update record. True means insert. The default is false and not insert.
Multi: optional. Mongodb is false by default. Only the first record found will be updated. If this parameter is true, all the records found by conditions will be updated.
Writeconcern: optional, the level of exception thrown.
db.collection.updateOne()
Updates a single document to the specified collectiondb.collection.updateMany()
Updates multiple documents to the specified collection
example:
db.COLLECTION_NAME.update(
{'title':'mongodb tutorial '},
{$set:{'title':'MongoDB'}},
{multi:true}
)
save
db.collection.save(
<document>,
{
writeConcern: <document>
}
)
Document: document data.
Writeconcern: optional, the level of exception thrown.
replace
db.collection.replaceOne()
remove document
db.COLLECTION_NAME.deleteMany()
: delete a single documentdb.COLLECTION_NAME.deleteOne()
: delete multiple documents
consult your documentation
db.COLLECTION_NAME.find({<field1>: <value1>, <field1>: { <operator1>: <value1> }})
: field refers to the name and operator refers to the operatordb.collection.findOne()
example
db.inventory.find( { status: "D" } )
Equivalent toSELECT * FROM inventory WHERE status = "D"
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
Equivalent toSELECT * FROM inventory WHERE status in ("A", "D")
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
Equivalent toSELECT * FROM inventory WHERE status = "A" AND qty < 30
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
Equivalent toSELECT * FROM inventory WHERE status = "A" OR qty < 30
More operator related viewsThis document
Query embedded objects using identifiers.
db.inventory.find( { "size.uom": "in" } )
References
Rookie tutorial getting started with mongodb
Runnob mongodb getting started
Mongodb Chinese Manual
Official course
For more development techniques, interview tutorials and Internet Co push, please welcome my WeChat official account. Benefits will be paid from time to time~