开发者

MongoDB Dot-Notation Query

开发者 https://www.devze.com 2023-01-24 00:42 出处:网络
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...

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 JS开发者_如何学GoON, 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!


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?


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 ...


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

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

精彩评论

暂无评论...
验证码 换一张
取 消