Base MongoDB (Advanced)

Source: Internet
Author: User
Tags mongoclient mongodb

MongoDB BasicsStage One, understanding MongoDB1, the basic form of data of MongoDB organization

MongoDB ———— Database ———— "collection ————" documentation

MySQL: Table: Rows and Columns: Fields

Application scenarios: such as real-time information about the delivery rider, logs, game character attributes, nearby people information

Application features: Real-time change of QPS

Data type: Each document is stored as a key-value pair

2. Enter and exit the database

Entry: MONGO

Exit: Exit

Stage II, library, set operation

1. Show all libraries: Shou DBS

2. Switch database: Use database name

3. View your library: DB

4. Delete library: Db.dropdatabase ()

5. View the collection inside the current library: Show collections

6. Create a collection: db.createcollection (' student ')

7. Delete collection: Db. Collection name. Drop ()

Phase III, data deletion and modification of the operation1. Inserting Data

Db. Collection name. Insert (document)

Each piece of data is a document, which is a JSON

When inserting a document, MongoDB assigns a unique objectid to the document if the _ID parameter is not specified

#example: Inserting aDb.student.insert ({name: ' Shiwei', age:18})#Add ID when insertingDb.student.insert ({_id:10,name: ' Shiwei', age:18})?#insert more than one:Db.student.insert ([{name: ' Shiwei', Sex:'Male', age:18},{Name:'Zhang San', Sex: ' Female', age:30},{Name:'John Doe', Sex: ' Male', age:48},])
2. Query Data

Db. Collection name. Find ()

#Search All: Db.student.find ()#query all, format is indented: Db.student.find (). Pretty ()#Query by criteria:> Db.student.find ({name:'zcm'}){ "_id": ObjectId ("5b3789beccd791f53ba27b05"),"name":"zcm"," Age": 22 }{ "_id": ObjectId ("5b378bc6ccd791f53ba27b07"),"name":"zcm"," Age": 22 }?> Db.student.find ({name:'zcm'},{age:1}){ "_id": ObjectId ("5b3789beccd791f53ba27b05")," Age": 22 }{ "_id": ObjectId ("5b378bc6ccd791f53ba27b07")," Age": 22 }?> Db.student.find ({name:'zcm'},{age:0}) {"_id": ObjectId ("5b3789beccd791f53ba27b05"),"name":"zcm" }{ "_id": ObjectId ("5b378bc6ccd791f53ba27b07"),"name":"zcm"}
3. Update Data

Update data

Db. Collection name. Update (

<query>, (conditions)

<update>,

{multi: <boolean>}

? )

# Full document Update: db.stu.update ({name: ' Shiwei '},{xx: ' yy '})? # specifies that the property update, through the operator $set, has the same property, only changes one data db.student.update ({name: 'zcm'},{$set: {Age :)? # Update multiple: {multi:ture}   . (If only field updates are specified)db.student.update ({name:'zcm'},{$set: {age:5}},{multi:true})
4. Delete Data

Db. Collection name. Remove (

? <query>, (conditions)

<justOne>

? )

# just delete a piece of data db.student.remove ({xx:'yy'},{justone:true})? # Delete all data that satisfies the condition:db.student.remove ({name:'zcm'})
stage four, python operation MongoDB1. Connector (drive)

Install Python package: Pip install Pymongo

Introducing Package Pymongo:import Pymongo

Establish the connection and create the client: client= Pymongo. Mongoclient (' 127.0.0.1 ', 27017)

Specify database: db=client[database name]

Specified collection: stu=db [collection name]

2. Main methods

Insert_one

Insert_many

Update_One

Update_many

Delete_one

Delete_many

Find_one

Find

3. Example
ImportPymongo?#Establish a connectionClient = Pymongo. Mongoclient ('127.0.0.1', 27017)?#get the database to manipulatedb = client['Student']?#gets the collection to manipulateCol = db['Student']?data=Col.find ()Print(data)#an object was printed to prove the connection was successful.?#Insert a piece of data#col.insert_one ({' name ': ' zcm ', ' Age ': $})?#inserting more than one data#Li = [#{' Nane ': ' Zhangsan ', ' age ': +, ' sex ': ' Nan '},#{' Nane ': ' Baby ', ' age ':, ' sex ': ' NV '},#{' Nane ': ' Dengchao ', ' age ': +, ' sex ': ' Nan '}# ]#Col.insert_many (LI)?#update One piece of data#col.update_one ({' name ': ' zcm '}, {' $set ': {' Age ': 3}})?#update more than one data#col.update_many ({' name ': ' zcm '},{' $set ': {' age ': +}})?#querying a single piece of data#find = Col.find_one ()#print (Find)?#querying multiple data returns only objects#find_d = Col.find ()#print (find_d)?#Delete a piece of data#col.delete_one ({' name ': ' zcm '},{' justone ': ' true '})?#Delete more than one piece of data#col.delete_many ({' name ': ' zcm '})

Base MongoDB (Advanced)

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.