MongoDB 点表示法查询

发布于 2024-10-01 13:23:03 字数 1297 浏览 7 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-10-08 13:23:03

我已经测试了这些查询,无论是在 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?

昇り龍 2024-10-08 13:23:03

如果您经常处理子文档查询的查询,我建议您查看 $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 ...

挽手叙旧 2024-10-08 13:23:03

您插入的 json 对象有 (u')。这就是问题所在,XML 到 JSON 的转换不正确。

 u'message:u'{category:{text:"scores"}

your inserted json object has (u'). This is the problem, XML to JSON conversion is incorrect.

 u'message:u'{category:{text:"scores"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文