pymongo

Mongodb is returning wierdly formatted data

☆樱花仙子☆ 提交于 2020-01-16 18:29:44
问题 When i try to get this data from my mongodb database using flask-restful and pymongo i get some wierdly formatted data. For example. This is what the data looks like in the database. { "_id" : ObjectId("5217f3cc7466c06862c4a4f7"), "Hello" : "World" } This is what it looks like when it gets returned from the database. "{\"_id\": {\"$oid\": \"5217f3cc7466c06862c4a4f7\"}, \"Hello\": \"World\"}" Using this code: def toJSON(data): return json.dumps(data, default=json_util.default) And this: def

Querying for array in embedded list

谁都会走 提交于 2020-01-16 05:21:06
问题 Supposed I have a mongo document that looks similar to the following: { 'foo':1 'listOfLists' : [ [1,2],[3,4] ] } (Yes I am aware this isn't how it "really" looks but it should be simple enough for explanation purposes.) If I wanted to write a query that would check to see if the listsOfLists list object contains the combination of [3,4], how could I go about doing that? Could I do something like collection.find({'listsOfLists' : {'$elemMatch' : [3,4] } }) 回答1: collection.find({ 'listsOfLists

Retrieve data from mongodb based on date condition in pymongo

夙愿已清 提交于 2020-01-15 13:21:06
问题 My MongoDB document is as follows: - { "_id" : 111, "ts" : [ ISODate("2010-08-21T23:13:35.000Z"), ISODate("2011-08-23T09:13:10.000Z"), ISODate("2012-08-26T12:55:51.000Z"), ISODate("2010-08-29T03:11:12.000Z") ] } { "_id" : 112, "ts" : [ ISODate("2010-08-21T23:13:35.000Z"), ISODate("2011-08-23T09:13:10.000Z"), ISODate("2012-08-26T12:55:51.000Z"), ISODate("2010-08-29T03:11:12.000Z") ] } Each date entry indicates a "hit". I am trying to count the number of hits each id had between a certain date

Retrieve data from mongodb based on date condition in pymongo

一个人想着一个人 提交于 2020-01-15 13:21:05
问题 My MongoDB document is as follows: - { "_id" : 111, "ts" : [ ISODate("2010-08-21T23:13:35.000Z"), ISODate("2011-08-23T09:13:10.000Z"), ISODate("2012-08-26T12:55:51.000Z"), ISODate("2010-08-29T03:11:12.000Z") ] } { "_id" : 112, "ts" : [ ISODate("2010-08-21T23:13:35.000Z"), ISODate("2011-08-23T09:13:10.000Z"), ISODate("2012-08-26T12:55:51.000Z"), ISODate("2010-08-29T03:11:12.000Z") ] } Each date entry indicates a "hit". I am trying to count the number of hits each id had between a certain date

Insert Multiple JSON files to MongoDB using python

北慕城南 提交于 2020-01-15 11:38:25
问题 The JSON files are as follows a.json,b.json.....z.json (26 json files) The json format of each of the file looks as: { "a cappella": { "word": "a cappella", "wordset_id": "5feb6f679a", "meanings": [ { "id": "492099d426", "def": "without musical accompaniment", "example": "they performed a cappella", "speech_part": "adverb" }, { "id": "0bf8d49e2e", "def": "sung without instrumental accompaniment", "example": "they sang an a cappella Mass", "speech_part": "adjective" } ] }, "A.D.": { "word": "A

mongoengine save method is deprecated?

寵の児 提交于 2020-01-15 10:11:49
问题 I wonder why my python says that mongoengine save() method is deprecated? I don't see any info about this into official documentation https://mongoengine.readthedocs.io/en/v0.9.0/apireference.html class MyModel(Document): user_id = StringField(required=True) date = DateTimeField(required=True, default=datetime.datetime.now) my = MyModel() my.user_id = 'user' my.save() and now i see: /Library/Python/2.7/site-packages/mongoengine/document.py:340: DeprecationWarning: save is deprecated. Use

PyMongo max_time_ms

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 09:55:08
问题 I would like to use the max_time_ms flag during a find on mongodb, but I woulld like to understand how this flag works and how to verify that it is working. pymongo find().max_time_ms(500) Is there any way to verify? I tried to db.fsyncLock() , but I get this is applicable only for inserts. I thought that a possible solution should be insert too many entries and reduce to max_time_ms(1) , so the query will not have enough time to take results. Any suggestions? Tks 回答1: Passing the max_time_ms

Efficiency when inserting into mongodb (pymongo)

∥☆過路亽.° 提交于 2020-01-15 09:31:08
问题 Updated for clarity: I need advice for performance when inserting/appending to a capped collection . I have two python scripts running: (1) Tailing the cursor. while WSHandler.cursor.alive: try: doc = WSHandler.cursor.next() self.render(doc) (2) Inserting like so: def on_data(self, data): #Tweepy if (len(data) > 5): data = json.loads(data) coll.insert(data) #insert into mongodb #print(coll.count()) #print(data) and it's running fine for a while (at 50 inserts/second). Then, after 20-60secs,

Map reduce execute failed with pymongo but success in mongo shell

十年热恋 提交于 2020-01-14 05:43:12
问题 I got a problem about mongo map reduce, my map reduce can execute success in mongo shell but when i try to execute use pymongo i got the following errors. The following json is my Collections JSON format sample { "_id" : ObjectId( "4e41661ecacbd10e00012600" ), "timestamp" : "20110809", "variants" : { "407" : { "number_of_ad_clicks" : 101, "number_of_search_keywords" : 20, "total_duration" : 4, "os" : { "os_2" : 2, "os_1" : 1, "os_0" : 0 }, "countries" : { "ge" : 2, "ca" : 7, "fr" : 2, "uk" :

How to use Distinct, Sort, limit with mongodb

时间秒杀一切 提交于 2020-01-13 06:01:44
问题 I have a document structure {'text': 'here is text', 'count' : 13, 'somefield': value} Collection has some thousands of record, and text key value may be repeated many time, I want to find distinct text with highest count value,along with that whole document should be returned , I am able to sort them in descending order. distinct returns unique value in a list. I want to use all three functions and document has to be returned, I am still learning and not covered mapreduce. 回答1: Can you