Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a pymongo.cursor.Cursor into a dict?

I am using pymongo to query for all items in a region (actually it is to query for all venues in a region on a map). I used db.command(SON()) before to search in a spherical region, which can return me a dictionary and in the dictionary there is a key called results which contains the venues. Now I need to search in a square area and I am suggested to use db.places.find, however, this returns me a pymongo.cursor.Cursor class and I have no idea how to extract the venue results from it.

Does anyone know whether I should convert the cursor into a dict and extract the results out, or use another method to query for items in a square region? BTW, db is pymongo.database.Database class

The codes are:

>>> import pymongo >>> db = pymongo.MongoClient(host).PSRC  >>> resp = db.places.find({"loc": {"$within": {"$box": [[ll_lng,ll_lat], [ur_lng,ur_lat]]}}}) >>> for doc in resp: >>>     print(doc) 

I have values of ll_lng, ll_lat, ur_lng and ur_lat, use these values but it prints nothing from this codes

like image 315
gladys0313 Avatar asked Mar 10 '15 16:03

gladys0313


People also ask

How do I access the PyMongo cursor?

Manually iterating a cursor. In MongoDB, the find() method return the cursor, now to access the document we need to iterate the cursor. In the mongo shell, if the cursor is not assigned to a var keyword then the mongo shell automatically iterates the cursor up to 20 documents.

What is PyMongo cursor object?

As we already discussed what is a cursor. It is basically a tool for iterating over MongoDB query result sets. This cursor instance is returned by the find() method.

How can I tell if PyMongo cursor is empty?

Check if the Cursor object is empty or not? Approach 1: The cursor returned is an iterable, thus we can convert it into a list. If the length of the list is zero (i.e. List is empty), this implies the cursor is empty as well.

What does PyMongo find return?

PyMongo read all data The find method selects documents in a collection or view and returns a cursor to the selected documents.


2 Answers

The find method returns a Cursor instance, which allows you to iterate over all matching documents.

To get the first document that matches the given criteria you need to use find_one. The result of find_one is a dictionary.

You can always use the list constructor to return a list of all the documents in the collection but bear in mind that this will load all the data in memory and may not be what you want.

You should do that if you need to reuse the cursor and have a good reason not to use rewind()


Demo using find:

>>> import pymongo >>> conn = pymongo.MongoClient() >>> db = conn.test #test is my database >>> col = db.spam #Here spam is my collection >>> cur = col.find()   >>> cur <pymongo.cursor.Cursor object at 0xb6d447ec> >>> for doc in cur: ...     print(doc)  # or do something with the document ...  {'a': 1, '_id': ObjectId('54ff30faadd8f30feb90268f'), 'b': 2} {'a': 1, 'c': 3, '_id': ObjectId('54ff32a2add8f30feb902690'), 'b': 2} 

Demo using find_one:

>>> col.find_one() {'a': 1, '_id': ObjectId('54ff30faadd8f30feb90268f'), 'b': 2} 
like image 119
styvane Avatar answered Sep 19 '22 18:09

styvane


Easy

import pymongo conn = pymongo.MongoClient() db = conn.test #test is my database col = db.spam #Here spam is my collection array = list(col.find())  print(array) 

There you go

like image 23
Aminah Nuraini Avatar answered Sep 20 '22 18:09

Aminah Nuraini