MongoDB Common operations

Source: Internet
Author: User
Tags mongoclient

relational database nouns vs. MongoDB:

relational database Mongodb
Database Database
Table Collection
Row Document
Index Index
Join Lookup
Foreign Key Reference
multi-table Transaction Single Document transaction


The command line uses MongoDB

Insert your First Data

> Show Databases

Local 0.000GB

> Use test #切换到test数据库, if not, create a new

Switched to DB test

> Show Databaseslocal 0.000GB

> Db.demo.insert ({"Key": "Value"}) Writeresult ({"ninserted": 1})

> Show databaseslocal 0.000GBtest 0.000GB

> Show Collectionsdemo

> Db.demo.findOne () {"_id": ObjectId ("573af7085ee4be80385332a6"), "key": "Value"}


Using MongoDB in Python

Import pymongo## client defaults to localhost and port 27017. eg  mongoclient (' localhost ',  27017) Client = pymongo. Mongoclient () #连接到本地数据库blogDatabase  = client[  "blog"  ]     # Switch to the blog database userscollection = blogdatabase[  "users"  ]    # Switch to Userscollectionuserscollection.insert_one ( {  "username"  :  "jdrumgoole", "Password"   :  "Top secret", "Lang"  :  "EN"  }) #插入一条数据user  = userscollection.find_one () # Find the latest piece of data print ( user ) articlescollection = blogdatabase[  "articles"  ]author  =  "Jdrumgoole" article = {  "title"  :  "this is my first " Post "," Body " : " The is the longer body text for my blog  post. we can add lots of text here. "," author" : author," tags " : [ " Joe ", " General ", " Ireland ", " admin " ]}##  lets check if our author exists#if userscollection.find_one ( {  " Username " : AUTHOR })  :articlescollection.insert_one ( article ) else:raise  ValueError (  "Author %s does not exist"  % author )


This article is from the "Danielqu" blog, make sure to keep this source http://qujunorz.blog.51cto.com/6378776/1858528

MongoDB Common operations

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.