ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MongoDB学习笔记5——Python和MongoDB

2020-03-11 12:56:28  阅读:263  来源: 互联网

标签:Tags Python MongoDB collection ItemNumber 笔记 doc find Location


1. MongoDB使用BSON样式的文档,在Python中使用的是字典。

2.使用PyMongo模块

1)连接和断开

from pymongo import MongoClient

c = MongoClient()

db = c.library (其中library是数据库)

collection = db.items (items是集合)

2)插入数据

item = {
"Type" : "Laptop",
"ItemNumber" : "1234EXD",
"Status" : "In use",
"Location" : {
"Department" : "Development",
"Building" : "2B",
"Floor" : 12,
"Desk" : 120101,
"Owner" : "Andreson, Thomas"
},
"Tags" : ["Laptop","Development","In Use"]
}

将数据插入到集合中:

collection.insert_one(item)

如果遇到多组数据的情况:

two = [{
"Type" : "Laptop",
"ItemNumber" : "2345EXD",
"Status" : "In use",
"Location" : {
"Department" : "Development",
"Building" : "2B",
"Floor" : 12,
"Desk" : 120102,
"Owner" : "Smith, Simon"
},
"Tags" : ["Laptop", "Development", "In Use"]
},
{
"Type" : "Laptop",
"ItemNumber" : "3456TFS",
"Status" : "In use",
"Location" : {
"Department" : "Development",
"Building" : "2B",
"Floor" : 12,
"Desk" : 120103,
"Owner" : "Smith, Simon"
},
"Tags" : ["Laptop", "Development", "In Use"]
}]

使用

collection.insert_many(two)

3)搜索数据

3-1)搜索单个文档

collection.find_one() (没有其他限制条件默认返回第一个文档)

collection.find_one({"ItemNumber":"3456TFS"}, {'_id':False})(有限制条件,不显示'_id')

3-2)搜索多个文档

for doc in collection.find():

doc

for doc in collection.find({"Location.Owner":"Andreson, Thomas"}):

doc

3-3)使用点操作符

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

doc

3-4)返回字段

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

doc

3-5)使用sort()、limit()和skip()简化查询

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

doc

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

for doc in collection.find({'Status':'In use'},{'ItemNumber':True,'Location.Owner':True}).limit(2).skip(1).sort('ItemNumber'): (跳过一个,输出两个,按照要求排序)
doc

3-6)聚集查询

3-6-1)使用count()统计数目

collection.count()

3-6-2)使用distinct()统计唯一数据的数目

collection.distinct('ItemNumber') (无重复)

3-6-3)使用聚集框架对数据分组

collection.aggregate([
{'$unwind':'$Tags'},
{'$group':{'_id':'$Tags','Totals':{'$sum':1}}}
])

按照特定指示输出数据

from bson.son import SON
collection.aggregate([
{'$unwind':'$Tags'},
{'$group':{'_id':'$Tags','Totals':{'$sum':1}}},
{'$sort':SON([('Totals',-1)])}
])

3-7)使用hint()指定索引

from pymongo import ASCENDING

collection.create_index([("ItemNumber",ASCENDING)])

for doc in collection.find({"Location.Owner":"Smith, Simon"}).hint([("ItemNumber",ASCENDING)]):
doc

3-8)使用条件操作符定义查询

3-8-1)使用$lt、$gt、$lte和$gte操作符

for doc in collection.find({"Location.Desk":{"$lt":120102}}):
doc

for doc in collection.find({"Location.Desk":{"$gt":120102}}):
doc

3-8-2)使用$ne搜索不匹配的数据

for doc in collection.find({"Status":{"$ne":"In use"}}):
doc

3-8-3)使用$in搜索不匹配的数据

for doc in collection.find({"Tags":{"$in":["Not used","Development"]}},{"ItemNumber": True}).limit(2)

doc

3-8-4)使用$nin指定不匹配的数组

for doc in collection.find({"Tags":{"$nin":["Development"]}},{"ItemNumber":True}):

doc

3-8-5)搜索匹配数组值的文档

for doc in collection.find({"Tags":{"$all":["Storage","Not used"]}},{"ItemNumber":True}):

doc

3-8-6)使用$or指定多个匹配表达式
for doc in collection.find({"$or":[{"Location.Department":"Storage"},{"Location.Owner":"Anderson, Thomas"}]}):

doc

3-8-7)使用$slice从数组中获取元素

重新输入一组数据

chair = ({
"Status":"Not used",
"Tags":["Chair","Not used","Storage"],
"ItemNumber":"6789SID",
"Location":{
"Department":"Storage",
"Building":"2B"
},
"PreviousLocation":["120100","120101","120102","120103","120104","120105","120106","120107","120108","120109","120110"]
})

collection.insert_one(chair)

collection.find_one({'ItemNumber':'6789SID'},{'PreviousLocation':{'$slice':3}})

collection.find_one({'ItemNumber':'6789SID'},{'PreviousLocation':{'$slice':-3}})

 collection.find_one({'ItemNumber':'6789SID'},{'PreviousLocation':{'$slice':[5,3]}})

3-8-9)使用正则表达式执行搜索

import re

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

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

for doc in collection.find({'Location.Owner':re.compile('^anderson.',re.IGNORECASE)},{'ItemNumber':True,'Location.Owner':True}):
doc

3-9)修改数据

3-9-1)更新数据

update = ({
"Type":"Chair",
"Status":"Not used",
"Tags":["Chair","Not used","Storage"],
"ItemNumber":"6789SID",
"Location":{
"Department":"Storage",
"Building":"2B",
"DeskNumber":131131,
"Owner":"Martin, Lisa"
}
})

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

3-9-2)修改操作符

使用$inc增加整数值:

collection.update_one({"ItemNumber":"6789SID"},{"$inc":{"Location.DeskNumber":20}})

使用$set修改现有值:

collection.update_many({"Location.Department":"Development"},{"$set":{"Location.Building":"3B"}})

使用$unset移除键/值字段:

collection.update_one({"Status":"Not used","ItemNumber":"2345FDX"},{"$unset":{"Location.Building":1}})

使用$push向数组中添加值

collection.update_many({"Location.Owner":"Anderson, Thomas"},{"$push":{"Tags":"Anderson"}})

使用$push和$each向数组中添加多个值

collection.update_one({"Location.Owner":re.compile("^Walker,")},{"$push":{'Tags':{'$each':['Walker','Warranty']}}})

使用$addToSet向现有数组中添加值

 collection.update_many({"Type":"Chair"},{"$addToSet":{"Tags":"Warranty"}})

使用$pop从数组中删除元素

删除第一个元素

collection.update_one({"type":"Chair"},{"$pop":{"Tags":-1}})

删除最后一个元素

collection.update_one({"type":"Chair"},{"$pop":{"Tags":1}})

使用$pull删除特定的值(全部删除)

collection.update_one({"type":"Chair"},{"$pull":{"Tags":"Double"}})

使用replace_one()代替文档

collection.repalce_one(Desktop, NewDocument, upsert=True)

4)批处理数据

bulk = collection.initialize_ordered_bulk_op()

bulk.insert({"Type":"Laptop","ItemNumber":"2345EXD","Status":"Available"})

5)删除数据

删除文档

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

删除集合

db.items.drop()

删除数据库

c.drop_datebase("library")

6)在两个文档之间建立链接

在PyMongo中,数据库引用(DBRef)是通过DBRef模块中的DBRef()函数实现的,它可在处于不同位置的两个文档之间创建链接。

jan = {
"First Name" : "Jan",
"Last Name" : "Walker",
"Display name" : "Walker, Jan",
"Department" : "Development",
"Building" : "2B",
"Floor" : 12,
"Desk" : 120103,
"E-mail" : "jw@example.com"
}

people = db.people

people.insert(jan)

laptop = {
"Type" : "Laptop",
"Status" : "In use",
"ItemNumber" : "12345ABC",
"Tags" : ["Warranty","In use","Laptop"],
"Owner" : jan["_id"]
}

items = db.items

items.insert_one(laptop)

from bson.dbref import DBRef

mike = {
"First name" : "Mike",
"Last name" : "Wazowski",
"Display name" : "Wazowski, Mike",
"Department" : "Entertainment",
"Building" : "2B",
"Floor" : 10,
"Desk" : 120789,
"E-Mail" : "mw@monsters.inc"
}

people.insert_one(mike)

laptop = {
"Type" : "Laptop",
"Status" : "In use",
"ItemNumber" : "2345DEF",
"Tags" : ["Warranty","In use","Laptop"],
"Owner" : DBRef('people',mike["_id"])
}

items.insert_one(laptop)

person = items.find_one({"ItemNumber":"2345DEF"})

db.dereference(person["Owner"])

标签:Tags,Python,MongoDB,collection,ItemNumber,笔记,doc,find,Location
来源: https://www.cnblogs.com/zhuozige/p/12461666.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有