Python和MongoDB

1.链接和断开

from pymongo import MongoClient   #导入PyMongo驱动中的MongoClient模块

c = MongoClient() #建立与数据库的连接

db = c.test #使用字典c引用该连接,选择test数据库

collection = db.items #使用db选择集合名为items的集合

2.插入数据

collection.insert_one(item) #item是已经定义好的文档,结果返回一个ObjectId值

同时插入多个文档(通过在单个字典中指定多个文档):collection.insert_many(two) #two为定义好的文档,结果返回两个个ObjectId值

3.搜索数据

find_one()用于搜索集合中匹配搜索条件的单个文档

find()可基于所提供的参数搜索多个文档(如果不指定参数,find()将返回集合中的所有文档)

搜索单个文档:

collection.find_one() #若未使用参数,默认返回集合中的第一个文档

假设希望搜索ItemNumber值为3456TFS的文档,并且不希望返回文档的_id:collection.find_one({"ItemNumber": "3456TFS"}, fields={'_id': False})

搜索多个文档:

  for doc in collection.find({"Location.Owner":"Walker, Jan"}):

    doc

4.点操作符:可用于搜索嵌入对象中的匹配元素;使用时只需要指定内嵌对象中一个数据项的键名

  for doc in collection.find({"Location.Department":" Development"}):

    doc

5.返回字段

如果文档较大,并且不希望返回文档存储的所有键值对:

for doc in collection.find({'Status': 'In use'}, Projection={'ItemNumber': True, 'Location.Owner' : True})

  doc

True表示返回,False表示隐藏

6.使用sort()、limit()和skip()简化查询(可以同时使用)

sort():按特定的键对结果进行排序

for doc in collection.find({'Status': 'In use'}, fields={'ItemNumber': True, 'Location.Owner': True}).sort('ItemNumber'):

  doc

limit():限制返回结果的总数

只返回在集合中搜索到的头两个文档,并且只返回ItemNumber(其中未指定搜索条件):

for doc in collection.find({}, {'ItemNumber':True}).limit(2):

  doc

skip():忽略前n个文档,返回匹配查询的其余文档

for doc in collection.find({}, {'ItemNumber':True}).skip(2):

  doc

7.聚集查询

使用count()统计数目:collection.count();更准确的统计查询:collection.find({"Status": "In use", "Location.Owner": "Walker, Jan"}).count()

使用distinct()统计唯一数据的数目:(获得不包含重复数据的准确统计)collection.distinct("参数")

使用聚集框架对数据分组:

使用aggregate()函数最强大的管道操作符$group将添加的文档按照标签进行分组,然后使用$sum聚集表达式基于它进行统计:

    collection.aggregate([{

      '$unwind': '$Tags'},

      {'$group': {'_id': 'Tags', 'Totals' : {'$sum': 1}}}

    ])

首先,aggregate()函数使用$unwind管道操作符,根据文档的'$Tags'数组(注意必须在名字中使用$)创建了一个文档流。接下来,调用$group管道操作符,将所有唯一的标签的值用作它们的'_id',创建了一个单行以及它们的总数--使用$group的$sum表达式计算'Totals'值。

将结果排序,通过添加管道操作符$sort实现,需要导入SON模块:from bson.son import SON

    collection.aggregate([{

      '$unwind': '$Tags'},

      {'$group': {'_id': 'Tags', 'Totals' : {'$sum': 1}}},

      {'$sort': SON([('Totals', -1)])}   #对Totals的值降序排序

    ])

8.使用条件操作符重定义查询(略)

9.使用正则表达式执行搜索

import re

#搜索ItemNumber含有4的文档

for doc in collection.find({'ItemNumber': re.compile('4')}, {"ItemNumber" : True}):

  doc

10.更新数据

update_one(arg, doc):用于更新一个匹配的文档

update_many(arg, doc):用于更新任意匹配的文档

arg参数指定了用于匹配文档的键值信息,doc参数包含了更新后的信息

例如:

#定义更新数据

update=({....})

#执行更新

collection.update_one({"ItemNumber": "6789SID"}, update)

#检查更新结果

collection.find_one({"Type": "Chair"})

11.修改操作符(略)

12.批处理数据(略)

13.删除数据

  collection.delete_one({"Status":"In use"})

14.在两个文档之间创建链接(略)

原文地址:https://www.cnblogs.com/sujianyun/p/8658007.html