Why do I get a pymongo.cursor.Cursor when trying to query my mongodb db via pymongo?

2024/9/20 17:34:32

I have consumed a bunch of tweets in a mongodb database. I would like to query these tweets using pymongo. For example, I would like to query for screen_name. However, when I try to do this, python does not return a tweet but a message about pymongo.cursor.Cursor. Here is my code:

import sys
import pymongo
from pymongo import Connection
connection = Connection()
db = connection.test
tweets = db.tweets
list(tweets.find())[:1]

I get a JSON, which looks like this:

{u'_id': ObjectId('51c8878fadb68a0b96c6ebf1'),u'contributors': None,u'coordinates': {u'coordinates': [-75.24692983, 43.06183036],u'type': u'Point'},u'created_at': u'Mon Jun 24 17:53:19 +0000 2013',u'entities': {u'hashtags': [],u'symbols': [],u'urls': [],u'user_mentions': []},u'favorite_count': 0,u'favorited': False,u'filter_level': u'medium',u'geo': {u'coordinates': [43.06183036, -75.24692983], u'type': u'Point'},u'id': 349223725943623680L,u'id_str': u'349223725943623680',u'in_reply_to_screen_name': None,u'in_reply_to_status_id': None,u'in_reply_to_status_id_str': None,u'in_reply_to_user_id': None,u'in_reply_to_user_id_str': None,u'lang': u'en',u'place': {u'attributes': {},u'bounding_box': {u'coordinates': [[[-79.76259, 40.477399],[-79.76259, 45.015865],[-71.777491, 45.015865],[-71.777491, 40.477399]]],u'type': u'Polygon'},u'country': u'United States',u'country_code': u'US',u'full_name': u'New York, US',u'id': u'94965b2c45386f87',u'name': u'New York',u'place_type': u'admin',u'url': u'http://api.twitter.com/1/geo/id/94965b2c45386f87.json'},u'retweet_count': 0,u'retweeted': False,u'source': u'<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>',u'text': u'Currently having a heat stroke',u'truncated': False,u'user': {u'contributors_enabled': False,u'created_at': u'Fri Oct 28 02:04:05 +0000 2011',u'default_profile': False,u'default_profile_image': False,u'description': u'young and so mischievious',u'favourites_count': 1798,u'follow_request_sent': None,u'followers_count': 368,u'following': None,u'friends_count': 335,u'geo_enabled': True,u'id': 399801173,u'id_str': u'399801173',u'is_translator': False,u'lang': u'en',u'listed_count': 0,u'location': u'Upstate New York',u'name': u'Joe Catanzarita',u'notifications': None,u'profile_background_color': u'D6640D',u'profile_background_image_url':           u'http://a0.twimg.com/profile_background_images/702001815/f87508e73bbfab8c8c85ebe10b29fcf6.png',u'profile_background_image_url_https':     u'https://si0.twimg.com/profile_background_images/702001815/f87508e73bbfab8c8c85ebe10b29fcf6.png',u'profile_background_tile': True,u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/399801173/1367200323',u'profile_image_url':     u'http://a0.twimg.com/profile_images/378800000012256721/d8b5f801fb331de6ead4aed42dc77a46_normal.jpeg',u'profile_image_url_https':   u'https://si0.twimg.com/profile_images/378800000012256721/d8b5f801fb331de6ead4aed42dc77a46_normal.jpeg'    ,u'profile_link_color': u'140DE0',u'profile_sidebar_border_color': u'FFFFFF',u'profile_sidebar_fill_color': u'E0F5A6',u'profile_text_color': u'120212',u'profile_use_background_image': True,u'protected': False,u'screen_name': u'JoeCatanzarita',u'statuses_count': 6402,u'time_zone': u'Quito',u'url': None,u'utc_offset': -18000,u'verified': False}}

However, when I try to query for this screen_name, I get:

tweets.find({"screen_name": "JoeCatanzarita"})
<pymongo.cursor.Cursor at 0x52c02f0>

And when I then try to count the number of tweets which have "screen_name": "name", I get:

tweets.find({"screen_name": "name"}).count()
0

Any idea what I am doing wrong/how I can get pymongo to return the tweets I am looking for?

Thanks!

Answer

PyMongo's find() method returns a Cursor. To actually execute the query on the server and retrieve results, iterate the cursor with list or a for loop:

for doc in tweets.find({'screen_name': 'name'}):print(doc)# Or:
docs = list(tweets.find({'screen_name': 'name'}))

If tweets.find({"screen_name": "name"}).count() returns 0, it means no documents match your query.

Edit: now that you've posted an example document, I see you want to query like:

list(tweets.find({'user.screen_name': 'name'}))

... since the screen_name field is embedded in the user sub-document.

https://en.xdnf.cn/q/72143.html

Related Q&A

using dropbox as a server for my django app

I dont know if at all i make any sense, but this popped up in my mind. Can we use the 2gb free hosting of dropbox to put our django app over there and do some hacks to run our app?

Proper overloading of json encoding and decoding with Flask

I am trying to add some overloading to the Flask JSON encoder/decoder to add datetime encoding/decoding but only succeeded through a hack.from flask import Flask, flash, url_for, redirect, render_templ…

How to check a specific type of tuple or list?

Suppose, var = (x, 3)How to check if a variable is a tuple with only two elements, first being a type str and the other a type int in python? Can we do this using only one check? I want to avoid this…

Cannot import name BlockBlobService

I got the following error:from azure.storage.blob import BlockBlobService ImportError: cannot import name BlockBlobServicewhen trying to run my python project using command prompt. (The code seems to…

Legend outside the plot in Python - matplotlib

Im trying to place a rather extensive legend outside my plot in matplotlib. The legend has quite a few entries, and each entry can be quite long (but I dont know exactly how long).Obviously, thats quit…

Filter items that only occurs once in a very large list

I have a large list(over 1,000,000 items), which contains english words:tokens = ["today", "good", "computer", "people", "good", ... ]Id like to get al…

Get Data JSON in Flask

Even following many example here & there, i cant get my API work in POST Method. Here the code about it :from flask import Flask, jsonify, request@app.route(/api/v1/lists, methods=[POST]) def add_e…

Commands working on windows command line but not in Git Bash terminal

I am trying to run certain commands in Git Bash but they continue to hang and not display anything. When I run them in the Windows command prompt they work.For example, in my windows command prompt the…

RBF interpolation: LinAlgError: singular matrix

The following call:rbf = Rbf(points[0], points[1], values,epsilon=2)results in an error:LinAlgError: singular matrixwith the following values:In [3]: points Out[3]: (array([71, 50, 48, 84, 71, 74, 89,…

What does `\x1b(B` do?

Im a Blessed user, and recently, when I tried to find out the contents of the term.bold() function, I got this output: \x1b[1m\x1b(B\x1b[mI understand what \x1b[1m and \x1b[m do, but what does \x1b(B d…