MongoDB 点表示法查询
我是 MongoDB 新手,在让点表示法查询工作时遇到问题...
我正在将 pymongo 与在远程 Amazon EC2 实例上运行的 MongoDB 结合使用...
而不是编写大量 XML 解析代码来提取大量数据不同的数据,我将 XML 转换为 JSON,将所有内容转储到 MongoDB,然后尝试使用点表示法查询提取我想要的数据片段...
数据被转换为 JSON 并插入到 Mongo 中。我可以看到所有插入内容(来自 python shell)...
db.feed.find() 中的项目: item
这是返回的示例项目...
{u'timestamp': datetime.datetime(2010, 11, 8, 20, 19, 55, 87000), u'message': u'{"category": {"text": "Scores"}, "XML_File_ID": {"text": "12292403"}, "game": {"status": {"text": "4 Qtr", "attrib": {"numeral": "4", "type": "P"}}, "time_r": {"text": "10:01"}, "vscore": {"text": "27"}, "vteam": {"text": "Pittsburgh", "attrib": {"id": "082"}}, "hteam": {"text": "Cincinnati", "attrib": {"id": "064"}}, "hscore": {"text": "14"}}, "seasontype": {"text": "Regular"}, "schedule_id": {"text": "3151"}, "location": {"city": {"text": "Pittsburgh"}, "state": {"text": "PA"}, "country": {"text": "USA"}}, "time_stamp": {"text": " November 8, 2010, at 11:19 PM ET "}, "game_id": {"text": "3151"}, "sport": {"text": "NFL"}, "heading": {"text": "BC-ABP+082:064* 27 14 4R10:01"}}', u'_id': ObjectId('4cd8cbebe8b5d58527000016')}
所以我尝试执行这样的查询,但没有得到任何结果...
db.feed.find_one({"message.category.text": “分数”})
执行这些类型的查询并将整个文档返回到响应中的正确方法是什么?谢谢!
I am new to MongoDB and I'm having trouble with getting my dot-notation queries to work...
I am using pymongo with MongoDB running on a remote Amazon EC2 instance...
Instead of writing massive XML parsing code to extract lots of different data, I am converting the XML to JSON, dumping everything into MongoDB, and then attempting to extract the pieces of data I want using dot-notation queries...
The data gets converted into JSON and inserted into Mongo fine. I can see all of the inserts (from the python shell)...
for item in db.feed.find():
item
Here is an example item that is returned...
{u'timestamp': datetime.datetime(2010, 11, 8, 20, 19, 55, 87000), u'message': u'{"category": {"text": "Scores"}, "XML_File_ID": {"text": "12292403"}, "game": {"status": {"text": "4 Qtr", "attrib": {"numeral": "4", "type": "P"}}, "time_r": {"text": "10:01"}, "vscore": {"text": "27"}, "vteam": {"text": "Pittsburgh", "attrib": {"id": "082"}}, "hteam": {"text": "Cincinnati", "attrib": {"id": "064"}}, "hscore": {"text": "14"}}, "seasontype": {"text": "Regular"}, "schedule_id": {"text": "3151"}, "location": {"city": {"text": "Pittsburgh"}, "state": {"text": "PA"}, "country": {"text": "USA"}}, "time_stamp": {"text": " November 8, 2010, at 11:19 PM ET "}, "game_id": {"text": "3151"}, "sport": {"text": "NFL"}, "heading": {"text": "BC-ABP+082:064* 27 14 4R10:01"}}', u'_id': ObjectId('4cd8cbebe8b5d58527000016')}
So I'm trying to do a query like this, but I'm not getting any results...
db.feed.find_one({"message.category.text": "Scores"})
What's the proper way to do these type of queries and get the whole document back in the response? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经测试了这些查询,无论是在 MongoDB shell 中还是使用 pymongo,它们都对我有用。您是否忘记将结果保存到变量和/或打印它们?
I've tested those queries, both from within the MongoDB shell and using pymongo and they work for me. Are you forgetting to save the results to a variable and/or print them?
如果您经常处理子文档查询的查询,我建议您查看 $elemmatch 。
“使用 $elemMatch 查询运算符 (mongod >= 1.3.1),您可以匹配数组中的整个文档。”
更多信息:官方文档 & 博客文章 ...
I'd suggest looking into $elemmatch if you are deailing with queries on subdocment queries a lot.
"Using the $elemMatch query operator (mongod >= 1.3.1), you can match an entire document within an array."
More here: official docs & a blog post ...
您插入的 json 对象有 (u')。这就是问题所在,XML 到 JSON 的转换不正确。
your inserted json object has (u'). This is the problem, XML to JSON conversion is incorrect.