pymongo连接mongodb数据库,并显示所有内容
# Requires pymongo 3.6.0+
from pymongo import MongoClient
client = MongoClient("mongodb://host:port/")
database = client["testMongo"]
collection = database["__schema__"]
query = {}
cursor = collection.find(query)
try:
for doc in cursor:
print(doc)
finally:
client.close()
数据库的结构:
{
"_id" : ObjectId("60d208b85a0f3d1157441896"),
"name" : "haipiya_compare_order_taobao",
"auto" : {
"field_names" : [
"id"
],
"seq" : NumberInt(1)
},
"fields" : {
"file_BBBT" : {
"type_code" : "string"
},
"file_BBZL" : {
"type_code" : "string"
},
"file_BBZSL" : {
"type_code" : "string"
}
}
}
查询:
查询名称
cursor = collection.find({'name':'haipiya_compare_order_taobao'})
查询子集,例如查询
cursor = collection.find({'auto.seq':1})
对查询的结果取值
cursor = collection.find({'name':'haipiya_compare_order_taobao'})
temp = list(cursor)
# 获取名称
print(temp[0]['name'])
对查询后的结果控制,例如不要fields字段,可以把fields字段设置成0,则查询集不显示fields字段。
cursor = collection.find({'auto.seq':1},{'fields':0})