Python Operation MongoDB

Source: Internet
Author: User
Tags mongoclient server port

An import Pymongo

From Pymongo import mongoclient

Two connection server port number 27017

Connect to MongoDB
Connect MongoDB We need to use the Pymongo library inside the mongoclient, generally incoming MongoDB IP and port, the first parameter is the address host, the second parameter is port ports, port if not pass the default is 27017.
conn = Mongoclient ("localhost")
Mongoclient (host= ' 127.0.0.1 ', port=27017)

Three-Connection database

DB = conn. database name
Connection Collection
Collection = Db[collection_name]
Or
Collection = Db.collection_name
View all aggregation names
Db.collection_names ()

Four insertion data (1) inserting a piece of data

Db.user.insert ({"Name": "Xiali just", "Age": "Hobby": "Learning"})

(2) inserting more than one data

Db.user.insert ([{"Name": "Xiali just", "Age": "Hobby": "Learning"},{"name": "Xxxoo", "Age": "Hobby": "Learning"}]

(3) Recommended for use above 3.x

Insert_one inserting a piece of data
Insert_many () inserting more than one data

(4) Return ID using Insert_one ()

data.inserted_id
Data.inserted_ids

Five query data (1) query all
db.user.find()#带条件的查询# data = db.user.find({"name":"周日"})# print(data) #返回result类似一个迭代器  可以使用 next方法 一个一个 的取出来# print(next(data))   #取出一条数据
(2) Query a
db.user.find_one()
(3) Query with conditions
db.user.find({"name":"张三"})
(4) Query ID
from bson.objectid import ObjectId*#用于ID查询data = db.user.find({"_id":ObjectId("59a2d304b961661b209f8da1")})
(5) Fuzzy query
(1){"name":{‘$regex‘:"张"}}(2)import re {‘xxx‘:re.compile(‘xxx‘)}
Six sort limit count skip (1) sort order

? Older than 10

data = db.user.find({"age":{"$gt":10}}).sort("age",-1) #年龄 升序 查询 pymongo.ASCENDING --升序data = db.user.find({"age":{"$gt":10}}).sort("age",1) #年龄 降序 查询 pymongo.DESCENDING --降序
(2) Limit value

? Fetch three data

db.user.find().limit(3)data = db.user.find({"age":{"$gt":10}}).sort("age",-1).limit(3)
(3) Count Statistics data Bar number
db.user.find().count()
(4) Skip starting from the first few data
db.user.find().skip(2)
Seven Update modifications

? The update () method is also the official deprecated method, here also the Update_One () method and the Update_many () method, the use of more stringent,

(1) Update ()
db.user.update({"name":"张三"},{"$set":{"age":25}})db.user.update({"name":"张三"},{"$inc":{"age":25}})
(2) Update_One () The first qualifying data to be updated
? db.user.update_one({"name":"张三"},{"$set":{"age":99}})
(3) Update_many () Update all eligible data
db.user.update_many({"name":"张三"},{"$set":{"age":91}})
(4) The return result is the Updateresult type, and then the Matched_count and Modified_count properties are called to get the number of matched data bars and the number of data bars affected.
print(result.matched_count, result.modified_count)没
Eight remove delete

Delete operation is simple, call the Remove () method to specify the condition of the deletion, all the data that matches the condition will be deleted.

(1) Delete Zhang San
collection.remove({"name":"lilei"})
(2) Delete all
collection.remove()
(3) There are still two new recommended methods, the Delete_one () and Delete_many () methods, as shown in the following example:
delete_one()即删除第一条符合条件的数据collection.delete_one({“name”:“ Kevin”})delete_many()即删除所有符合条件的数据,返回结果是DeleteResult类型collection.delete_many({“age”: {$lt:25}})
(4) You can call the Deleted_count property to get the number of deleted data bars.
result.deleted_count
Nine close connection
conn.close()

Python Operation MongoDB

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.