MongoDB Basics (2): MongoDB Collection and Document Operation Commands

insert image description here


MongoDB database common commands

1. Help to view the command prompt
help
db.help()
db.test.help()
db.test.find().help()
2. Create/switch database
> use music
switched to db music
3. Query the database
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
4. View the currently used database
> db
music
# 或
> db.getName()
music
5. Display the current DB status
> db.stats()
{
    
    
	"db" : "music",
	"collections" : 0,
	"views" : 0,
	"objects" : 0,
	"avgObjSize" : 0,
	"dataSize" : 0,
	"storageSize" : 0,
	"totalSize" : 0,
	"indexes" : 0,
	"indexSize" : 0,
	"scaleFactor" : 1,
	"fileSize" : 0,
	"fsUsedSize" : 0,
	"fsTotalSize" : 0,
	"ok" : 1
}
6. View the current DB version
db.version()
7. View the link machine address of the current DB
> db.getMongo()
connection to 127.0.0.1:27017
8. Delete the database
> db.dropDatabase()
{
    
     "ok" : 1 }

Collection collection operations

1. Create a collection
db.createCollection("singer", {
    
    size: 20, capped: true, max: 100});
2. Determine whether the set has a fixed capacity
> db.singer.isCapped()
true
3. Get the collection with the specified name
db.getCollection("account")
MongoDB delete collection
db.collection.drop()
4. Get all the collections of the current db
> db.getCollectionNames()
[ "album", "singer" ]
5. Display the status of all collections in the current db
db.printCollectionStats()

document manipulation

query all records

> db.singer.find()
{
    
     "_id" : ObjectId("622a15791181c7388318c06f"), "name" : "张三", "age" : 25 }
{
    
     "_id" : ObjectId("622a158d1181c7388318c071"), "name" : "zhangsan", "age" : 25, "sex" : true }
{
    
     "_id" : ObjectId("622a15961181c7388318c072"), "name" : "zhangsan", "age" : 25, "sex" : true }

Insert document

save

> db.singer.save({
    
    name: 'zhangsan', age: 25, sex: '男'})
WriteResult({
    
     "nInserted" : 1 })

> db.singer.find()
{
    
     "_id" : ObjectId("622a158d1181c7388318c071"), "name" : "zhangsan", "age" : 25, "sex" : '男' }

insert

> db.singer.insert([{
    
    name: '老王', age: 40}, {
    
    name: '老李', age: 30}])
BulkWriteResult({
    
    
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})

> db.singer.find()
{
    
     "_id" : ObjectId("622a17d61181c7388318c077"), "name" : "老王", "age" : 40 }
{
    
     "_id" : ObjectId("622a17d61181c7388318c078"), "name" : "老李", "age" : 30 }
insertOne
> db.singer.insertOne({
    
    name: '李四'})
{
    
    
	"acknowledged" : true,
	"insertedId" : ObjectId("622a16d51181c7388318c075")
}

> db.singer.find()
{
    
     "_id" : ObjectId("622a16bb1181c7388318c074"), "name" : "李四" }
insertMany
> db.singer.insertMany([{
    
    name: '小王', age: 20}, {
    
    name: '小李', age: 20}])
{
    
    
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("622a18c1e53b1497cfd6d1a3"),
		ObjectId("622a18c1e53b1497cfd6d1a4")
	]
}

> db.singer.find()
{
    
     "_id" : ObjectId("622a18c1e53b1497cfd6d1a3"), "name" : "小王", "age" : 20 }
{
    
     "_id" : ObjectId("622a18c1e53b1497cfd6d1a4"), "name" : "小李", "age" : 20 }

Modify the document update()

The update() method is used to update an existing document. The syntax format is as follows:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

Parameter Description:

  • query : The query condition for update, similar to the where in the sql update query.
  • update : the object of the update and some update operators (like , ,, inc...), etc., can also be understood as the set after the sql update query
  • upsert : optional, this parameter means, if there is no updated record, whether to insert objNew, true is the insertion, the default is false, not inserted.
  • multi : optional, mongodb defaults to false, only the first record found is updated, if this parameter is true, all the records found by the condition are updated.
  • writeConcern : optional, the level of exception thrown.

$set modification

db.singer.update({
    
    age: 25}, {
    
    $set: {
    
    name: 'changeName'}}, false, true);
# 相当于:
update singer set name = 'changeName' where age = 25;

$inc increments

db.singer.update({
    
    name: 'Lisi'}, {
    
    $inc: {
    
    age: 50}}, false, true);
# 相当于:
update singer set age = age + 50 where name = 'Lisi';
db.singer.update({
    
    name: 'Lisi'}, {
    
    $inc: {
    
    age: 50}, $set: {
    
    name: 'hoho'}}, false, true);
# 相当于:
update singer set age = age + 50, name = 'hoho'  where name = 'Lisi';

delete document

db.singer.remove({
    
    age: 132});

Guess you like

Origin blog.csdn.net/qq_41887214/article/details/123465890