MongoDB Learning Note--curd operation

Source: Internet
Author: User

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

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.