Mongodb Study Notes 02 and mongodb Study Notes

Source: Internet
Author: User

Mongodb Study Notes 02 and mongodb Study Notes
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 increases db. stu. update ({name: 'billvsme '}, {$ inc: {age: 2}) by 2
  • $ Rename: rename db. stu. update ({name: 'billvsme '}, {$ rename: {name: "username"}) to rename the name field to username
  • $ SetOnInsert: When upsert is enabled, set the field value.
  • $ Unset Delete specified column db. stu. update ({name: 'billvsme '}, {$ unset: {age: 1}) Delete the age Field

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.