Use of Python database Pymongo

Source: Internet
Author: User
Tags mongoclient mongodb

Two points must be guaranteed before use:

A MONGODB database is installed on the computer, and Python has the Pymongo package installed.

B Open Mongod.exe

1 Creating a Client

Client = Pymongo. Mongoclient (' localhost ', 27017)
#pymongo. Mongoclient () default is local client ' localhost ' when no parameters are taken: 27017

2 Correlation Database

Associated database There are two ways to create a database  and connect to an existing database: Create a database runoob:db = client[' Runoob ') connect an existing database runoocdb = CLIENT.RUNOOC

3 Association Collections

Associative collections have two ways of creating collections  and connecting existing collections: Creating collections Run:collection = Db[' run '] connecting existing collections Runocollection = Db.runo

4 Increase

#插入单条数据collection. Insert ({' name ': ' Lili ', ' age ': +}) #插入多条数据collection. Insert ({' name ': ' Lili ', ' age ': +}, {' Location ': [123,456], ' Dad ': ' Me '}

LST = [{' name ': ' Lili ', ' Age ': $}, {' Location ': [123,456], ' Dad ': ' Me '}]
Collection.insert (LST) #变量式doc = { ' name ': ' Lili ', ' age ': +, ' location ': [123,456], ' Dad ': ' Me ' }collection.insert (DOC)

5 Check

#单一条件查找for I in Collection.find ({' Age ': '):p rint i#& Find for I-collection.find ({' Age ': $, ' name ': ' Lili '}):p rint i# Or look for the for I in Collection.find ({' $or ': [{' {': ') ' ({' name ': ']} '):p rint i# & and OR with the Collection.find ({"likes": {' $gt ' : ' $or ': [{' Age ': $}, {' Age ': '} '}). #查一个具体对象collection. Find_one ({' Age ': ') ' #返回值是一个具体的对象

6 change

#更新一条数据collection. Update ({' Age ': $}, {' $set ': {' age ': +}}) #用 ' age ': 28 replace the found first ' age ': 24# update to record collection.update ({' Age ': $}, {' $set ': {' age ': Multi=true}}, and ' age ': 28 replaces all ' age ': 24

7 by deleting

#删掉所有 {' Age ': 28} Data Collection.remove ({' Age ': ') ' #删除数据集collection. Drop ()

8 Other

Sort: Use the sort () method in MongoDB to sort the data, the sort () method can specify the sorted field by parameter, and use 1 and-one to specify how to sort, where 1 is ascending, and 1 is descending.

For I in Collection.find (). Sort ([("Age", 1)]):    print (i)

Limit and Skip:

The #limit () method is used to read a specified number of data #skip () methods used to skip a specified number of data # The following means that after skipping two data, read 6 for I in My_set.find (). Skip (2). Limit (6):    print (i)

Condition operators for MongoDB:

#    (>)  greater than-$gt #    (<)  less than-$lt #    (>=)  greater than or equal to-$gte #    (<=)  less than or equal-$lte # Example: Querying all records of age greater than 25 in a collection for I in My_set.find ({"Age": {"$GT": +}}):    print (i)

Type (judging types)

#找出name的类型是String的  will only find out if name is a string type for I in My_set.find ({' name ': {' $type ': 2}}):    print (i) type table double    1     String    2     object    3     Array    4     Binary data    5     Undefined    6 deprecated    Object ID    7     Boolean    8     Date    9     Null     Regular Expression     One JavaScript     Symbol for     JavaScript (with scope)     32-bit integer     Timestamp     64-bit integer     Min key    255    Query With-1.max key    127

Use of Python database Pymongo

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.