1

Data :

{
  "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
}

I want to write queries in a single python script for all types of combinations in this data which gives me exact output what we want.

For example:
Suppose we want output only for fruit=Banana in Jalgaon district only, so that gives me exact output.

Suppose we want output in which district number of farmers are more than 100 , then that gives exact output.

Suppose we want output only for district=Pune, then that query gives exact output.likewise.

Any hints? How do I write queries to solve this?

Code I tried:

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
5
  • 1
    Did you already try something ? Commented Dec 19, 2016 at 9:08
  • 1
    You could load those struct as a dictionnary in python and then iterate over them with a list of criteria that you want. For exemple you iterate over all your element and print them if "fruit" == "Banana" etc. Commented Dec 19, 2016 at 9:09
  • Yes, I tried, 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 Commented Dec 19, 2016 at 9:10
  • Can you add this in your post, it will help to visualize your issue. Commented Dec 19, 2016 at 9:11
  • 2
    now it looks like a real question. Commented Dec 19, 2016 at 9:21

1 Answer 1

1

Use give code to find desired output. You can create various function for your different-2 query.

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()

You can take more help from this doc:

  1. https://docs.mongodb.com/manual/crud/
Sign up to request clarification or add additional context in comments.

5 Comments

@P,Madhukar Sir, code is gives error :File "stack1.py", line 9 query = "{"Fruit":"Guava"},{"Fruit":1}" ^ SyntaxError: invalid syntax
Just replace query in find funcation call. I made some change in my code. Try this
sir, code is working, but gives output "Guava" "Guava" ,
In this same python script how to write query for "Dist" --> "Pune" also, In same python script how to write query in which revenue_circle number of farmers are more than 100 ? any idea?
I projected only fruits. Refer this doc link docs.mongodb.com/manual/crud for your advance query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.