一: 数据结构
{ "_id" : ObjectId("5de8a5b748a75a8d48b72bdc"), "farm_id" : "2", "user_id" : "2", "equipment_number" : "2", "hybridization_detail" : [ { "hybridization_time" : "2019-11-11", "pregnancy _time" : "2019-12-4", "hybridization_type" : "冻精", "object_type" : "奶牛", "sperm_count" : "100", "sperm_number" : "1", "hybridization_control" : "性控", "id" : "15755278639220782" }, { "hybridization_time" : "2019-11-11", "pregnancy _time" : "2019-12-4", "hybridization_type" : "冻精", "object_type" : "奶牛", "sperm_count" : "100", "sperm_number" : "1", "hybridization_control" : "性控", "id" : "15755278639220783" } ] }
1. 普通查询 查询farm_id = 2的信息
find_one 返回的是一个字典类型的数据
from pymongo import MongoClient import logging lg = logging.getLogger("info") lg.setLevel(logging.DEBUG) class Mongodb_Wrapper(object): database_name = "test_function" collection_name = "normal" host = "127.0.0.1" port = 27017 def __init__(self): self.client = MongoClient(host=self.host,port=self.port) self.db = self.client[self.database_name][self.collection_name] def find_one(self): ret = self.db.find_one({"farm_id":"2"}) return ret m = Mongodb_Wrapper() ret = m.find_one() print(ret) print(type(ret)) # 结果 {'_id': ObjectId('5de8a5b748a75a8d48b72bdc'), 'farm_id': '2', 'user_id': '2', 'equipment_number': '2', 'hybridization_detail': [{'hybridization_time': '2019-11-11', 'pregnancy _time': '2019-12-4', 'hybridization_type': '冻精', 'object_type': '奶牛', 'sperm_count': '100', 'sperm_number': '1', 'hybridization_control': '性控', 'id': '15755278639220782'}, {'hybridization_time': '2019-11-11', 'pregnancy _time': '2019-12-4', 'hybridization_type': '冻精', 'object_type': '奶牛', 'sperm_count': '100', 'sperm_number': '1', 'hybridization_control': '性控', 'id': '15755278639220783'}]} <class 'dict'>
不想显示_id和farm_id怎么办?可以增加第二个参数,过滤条件
def find_one(self): ret = self.db.find_one({"farm_id":"2"},{"_id":0,"farm_id":0}) return ret 结果: {'user_id': '2', 'equipment_number': '2', 'hybridization_detail': [{'hybridization_time': '2019-11-11', 'pregnancy _time': '2019-12-4', 'hybridization_type': '冻精', 'object_type': '奶牛', 'sperm_count': '100', 'sperm_number': '1', 'hybridization_control': '性控', 'id': '15755278639220782'}, {'hybridization_time': '2019-11-11', 'pregnancy _time': '2019-12-4', 'hybridization_type': '冻精', 'object_type': '奶牛', 'sperm_count': '100', 'sperm_number': '1', 'hybridization_control': '性控', 'id': '15755278639220783'}]} <class 'dict'>
根据列表里面的字段进行查询
只想显示 "id" : "15755278639220782"的这条数据怎么办,通过限制 hybridization_detail的值进行查询,$elemMatch进行操作
情况一:唯一值字段 id
def find_one(self): ret = self.db.find_one({"hybridization_detail.id":"15755278639220782"},{"hybridization_detail":{"$elemMatch":{"id":"15755278639220782"}}}) return ret # 结果 {'_id': ObjectId('5de8a5b748a75a8d48b72bdc'), 'hybridization_detail': [{'hybridization_time': '2019-11-11', 'pregnancy _time': '2019-12-4', 'hybridization_type': '冻精', 'object_type': '奶牛', 'sperm_count': '100', 'sperm_number': '1', 'hybridization_control': '性控', 'id': '15755278639220782'}]} <class 'dict'>
但是不想显示_id字段,增加过滤条件
def find_one(self): ret = self.db.find_one({"hybridization_detail.id":"15755278639220782"},{"_id":0},{"hybridization_detail":{"$elemMatch":{"id":"15755278639220782"}}}) return ret # 结果:TypeError: skip must be an instance of int
情况二:不唯一值字段 id以外的所有字段:只显示匹配到的第一个字段,因此要保证数据的唯一性,就增加有唯一值的字段。
def find_one(self): ret = self.db.find_one({"hybridization_detail.sperm_count":"100"},{"hybridization_detail":{"$elemMatch":{"sperm_count":"100"}}}) return ret #结果:只显示匹配到的第一个数据 {'_id': ObjectId('5de8a5b748a75a8d48b72bdc'), 'hybridization_detail': [{'hybridization_time': '2019-11-11', 'pregnancy _time': '2019-12-4', 'hybridization_type': '冻精', 'object_type': '奶牛', 'sperm_count': '100', 'sperm_number': '1', 'hybridization_control': '性控', 'id': '15755278639220782'}]} <class 'dict'>
修改数据结构为
{ "_id" : ObjectId("5de8a5b748a75a8d48b72bdc"), "farm_id" : "2", "user_id" : "2", "equipment_number" : "2", "hybridization_detail" : [ { "hybridization_time" : "2019-11-11", "pregnancy _time" : "2019-12-4", "hybridization_type" : "冻精", "object_type" : "奶牛", "sperm_count" : "100", "sperm_number" : "1", "hybridization_control" : "性控", "id" : "15755278639220782" }, { "hybridization_time" : "2019-11-12", "pregnancy _time" : "2019-12-5", "hybridization_type" : "冻精", "object_type" : "奶牛", "sperm_count" : "100", "sperm_number" : "1", "hybridization_control" : "性控", "id" : "15755278639220783" }, { "hybridization_time" : "2019-11-13", "pregnancy _time" : "2019-12-6", "hybridization_type" : "冻精", "object_type" : "奶牛", "sperm_count" : "100", "sperm_number" : "1", "hybridization_control" : "性控", "id" : "15755278639220783" }, { "hybridization_time" : "2019-11-14", "pregnancy _time" : "2019-12-7", "hybridization_type" : "冻精", "object_type" : "奶牛", "sperm_count" : "100", "sperm_number" : "1", "hybridization_control" : "性控", "id" : "15755278639220783" } ] }
查询 "hybridization_time" 的时间从2019-11-11到2019-11-13的数据
# TODO