一:  数据结构

{
    "_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'>
View Code

相关文章: