数据:
{
"Fruit": "Pomegranate",
"District": "Nasik",
"Taluka": "Nasik",
"Revenue circle": "Nasik",
"Sum Insured": 28000,
"Area": 1200,
"Farmer": 183
}
{
"Fruit": "Pomegranate",
"District": "Jalna",
"Taluka": "Jalna",
"Revenue circle": "Jalna",
"Sum Insured": 28000,
"Area": 120,
"Farmer": 13
}
{
"Fruit": "Guava",
"District": "Pune",
"Taluka": "Haveli",
"Revenue circle": "Uralikanchan",
"Sum Insured": 50000,
"Area": 10,
"Farmer": 100
}
{
"Fruit": "Guava",
"District": "Nasik",
"Taluka": "Girnare",
"Revenue circle": "Girnare",
"Sum Insured": 50000,
"Area": 75,
"Farmer": 90
}
{
"Fruit": "Banana",
"District": "Nanded",
"Taluka": "Nandurbar",
"Revenue circle": "NandedBK",
"Sum Insured": 5000,
"Area": 2260,
"Farmer": 342
}
{
"Fruit": "Banana",
"District": "Jalgaon",
"Taluka": "Bhadgaon",
"Revenue circle": "Bhadgaon",
"Sum Insured": 5000,
"Area": 220,
"Farmer": 265
}
我想在一个python脚本中为这些数据中的所有类型的组合编写查询,这为我提供了我们想要的精确输出。
例如:
假设我们只想在Jalgaon区只为fruit = Banana输出,所以这给了我确切的输出。
假设我们想要输出农民的地区数量超过100,那么就可以得到准确的产出。
假设我们只想输出area = Pune,那么该查询会给出精确的输出。像。
任何提示?如何编写查询来解决此问题?
我试过的代码:
import pymongo
connection = pymongo.MongoClient("mongodb://localhost")
db=connection.book
record1 = db.book_collection
cursor = record1.find({"Fruit":"Banana"},{"Fruit":True, "_id":False, "Farmer":True})
for doc in cursor:
print doc # but there I need one python file for one query
答案 0 :(得分:1)
使用给定代码查找所需的输出。您可以为不同的2查询创建各种功能。
import pymongo
class DbQuery(object):
def __init__(self, ):
self.mongo_client = pymongo.MongoClient("localhost", 27017)
def findQuery(self):
mongo_db = self.mongo_client["book"]
cursor = mongo_db["book_collection"].find({"Fruit":"Guava"},{"Fruit":1})
for document in cursor:
# print document['District']
print document['Fruit']
if __name__ == '__main__':
obj = DbQuery()
obj.findQuery()
您可以从此文档中获得更多帮助: