草庐IT

python - 为什么在尝试通过 pymongo 查询我的 mongodb 数据库时得到 pymongo.cursor.Cursor?

coder 2023-10-27 原文

我在 mongodb 数据库中消费了一堆推文。我想使用 pymongo 查询这些推文。例如,我想查询 screen_name。但是,当我尝试这样做时,python 不会返回推文,而是返回有关 pymongo.cursor.Cursor 的消息。这是我的代码:

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

我得到一个 JSON,它看起来像这样:

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

但是,当我尝试查询此 screen_name 时,我得到:

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

然后当我尝试计算具有“screen_name”:“name”的推文数量时,我得到:

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

知道我做错了什么/我如何让 pymongo 返回我正在寻找的推文?

谢谢!

最佳答案

PyMongo 的 find() 方法返回一个 Cursor。要在服务器上实际执行查询并检索结果,请使用 list 或 for 循环迭代游标:

for doc in tweets.find({'screen_name': 'name'}):
    print(doc)

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

如果 tweets.find({"screen_name": "name"}).count() 返回 0,则表示没有文档匹配您的查询。

编辑:既然您已经发布了一个示例文档,我看到您想要这样查询:

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

...因为 screen_name 字段嵌入在 user 子文档中。

关于python - 为什么在尝试通过 pymongo 查询我的 mongodb 数据库时得到 pymongo.cursor.Cursor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17489789/

有关python - 为什么在尝试通过 pymongo 查询我的 mongodb 数据库时得到 pymongo.cursor.Cursor?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  3. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  4. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  5. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  6. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  7. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  8. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  9. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  10. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

随机推荐