MongoDB Learning Notes 02–curd operations
Curd represents create (create), update, read (read), and delete operations
Create a library
You can create a library by directly use the library name and then create collection
Create Collecion
db.createCollection("collectionName")
implicitly creating Collection
db.collectionName.insert({xxxxxx})
Delete Collection
db.collectionName.drop()
Deleting a database
db.dropDatabase()
Inserting data
db.collectionName.insert({xxxxxx})
The default is automatically added to the "_id" field, and if you don't want to automatically, you can specify the value of the "_id" field in the Insert data yourself
Multiple data can be inserted at one time
db.collectionName.insert(( {xxx },{xxx },{xxx }))
Delete data
db.collectionName.remote(查询表达式,选项)
option : {justone:true/false} default False
Db.stu.remove () Delete all the contents of the Stu document
Db.stu.remove ({name: ' billvsme '}) delete the information for name "Billvsme" under Stu document
Db.stu.remove ({name: ' Billvsme '}, True) plus true, indicating that only one row is deleted
Find data
db.stu.find(查询表达式,查询列)
Db.stu.find () Displays all data under the Stu document
Db.stu.find ({name: ' billvsme ') displays information for name "Billvsme" under Stu document
Db.stu.find ({},{name:true}) displays the Name column
Update data
db.collectionName.update(查询表达式,新值,选项)
option : {upsert:true/false,multi:true/false}
Upsert: If True, inserts a new value when the query does not exist and defaults to False
Multi: True to indicate that there is more than one match, the default is False
Db.stu.update ({name: ' billvsme '},{name: ' Zhengkai ', Age: ' 22 ') matches a message of name= ' Billvsme ' and then changes to {name: ' Zhengkai ', Age: ' 22 '} (note that the original content will be deleted, replaced, not modified)
update some items in the data
Db.stu.update ({name: ' billvsme '},{$set:{age:23}}) matches a message of name= ' Billvsme ' and then changes its age to 23
The $set is used here.
There are similar:
- $inc growth Db.stu.update ({name: ' billvsme '},{$Inc:{age:2}}) age increased by 2
- $Rename rename db.stu.update ({name: ' billvsme '},{$rename:{name: "Username"}}) Rename Name field to username
- $Setoninsert when Upsert, set the value of the field
- $unset Delete the specified column db.stu.update ({name: ' billvsme '},{$unset:{age:1}}) Delete the Age field
MongoDB Learning Note--curd operation