Mongodb Study Notes 02-CURD operations
Mongodb Study Notes 02-CURD operations
CURD indicates Create, Update, Read, and Delete operations.
Create a database
Directly use the database name and create a collection to create a database.
Create collecion
db.createCollection("collectionName")
Create collection implicitly
db.collectionName.insert({xxxxxx})
Delete collection
db.collectionName.drop()
Delete Database
db.dropDatabase()
Insert data
db.collectionName.insert({xxxxxx})
The "_ id" field is automatically added by default. If you do not want to automatically add it, you can specify the value of the "_ id" field in the inserted data.
Multiple data records can be inserted at a time.
db.collectionName.insert(( {xxx },{xxx },{xxx }))
Delete data
Db. collectionName. remote (query expression, option)
Option: {JustOne: true/false} default value: false
Db. stu. remove () delete all content in the stu document
Db. stu. remove ({name: 'billvsme '}) deletes all information named "billvsme" in the stu document.
Db. stu. remove ({name: 'billvsme '}, true) plus true, indicating that onlyOne row
Search for Data
Db. stu. find (query expression, query column)
Db. stu. find () displays all data in the stu document
Db. stu. find ({name: 'billvsme '}) displays information with the name "billvsme" in the stu document.
Db. stu. find ({},{ name: true}) display name Columns
Update Data
Db. collectionName. update (query expression, new value, option)
Option: {Upsert: true/false, multi: true/false}
Upsert: If this parameter is set to true, a new value is inserted when the query does not exist. The default value is false.
Multi: if it is true, it indicates multiple matches. The default value is false.
Db. stu. update ({name: 'billvsme '}, {name: 'zhengkai', age: '22'}) matched with 'billvsme'OneInformation, and then change it to {name: 'zhengkai', age: '22'} (note that the original content will be deleted, replaced, rather than modified)
Update certain items in Data
Db. stu. update ({name: 'billvsme '}, {$ set: {age: 23}) matches 'billvsme'OneInformation, and then change its age to 23
$ Set is used here.
There are also similar:
$ Inc growth db. stu. update ({name: 'billvsme '}, {$ inc: {age: 2}) add 2 $ rename to rename the db. stu. update ({name: 'billvsme '}, {$ rename: {name: "username"}) rename the name field to username $ setOnInsert. When upsert is enabled, set the field value $ unset to delete the specified column db. stu. update ({name: 'billvsme '}, {$ unset: {age: 1}) Delete the age Field