Questions tagged [pymongo]

PyMongo is the official Python driver for MongoDB created and maintained by MongoDB, Inc. Please mention the version of PyMongo you're using.

PyMongo is the official Python driver for MongoDB created and maintained by MongoDB, Inc.

Note: Since PyMongo 3 is not backward compatible with PyMongo 2 (refer to the changelog for details), you should specify the version you're using in the body of your question.

Documentation

Related Links

Related Tags

6819 questions
31
votes
2 answers

How to set connection timeout for Mongodb using pymongo?

I tried setting connectTimeoutMS and socketTimeoutMS to a low value but it still takes about 20 seconds before my script times out. Am I not using the options correctly? I want the script to exit after 5 seconds. def init_mongo(): …
sdot257
  • 10,046
  • 26
  • 88
  • 122
31
votes
4 answers

convert pymongo cursor to json

I know this is a fairly common problem. I'm writing a small Flask app and I'm trying to feed some queries back to the view. I've connected to my local MongoDB setup, and made a successful query - but I can't generate a json object with it. The…
sunwukung
  • 2,815
  • 3
  • 41
  • 56
29
votes
3 answers

Querying MongoDB (via pymongo) in case insensitive efficiently

I'm currently creating a website in python (pyramid) which requires users to sign up and log in. The system allows for users to choose a username which can be a mixture of capital letters, lowercase letters, and numbers. The problem arises when…
johneth
  • 2,858
  • 5
  • 27
  • 26
29
votes
1 answer

Can't get allowDiskUse:True to work with pymongo

I'm running into the aggregation result exceeds maximum document size (16MB) error with mongodb aggregation using pymongo. I was able to overcome it at first using the limit() option. However, at some point I got the Exceeded memory limit for…
David Makovoz
  • 1,766
  • 2
  • 16
  • 27
29
votes
3 answers

import pymongo causes `ImportError: cannot import name BSON`. how do you fix the import error?

I'm having trouble getting a simple Hello World app to work using Flask, MongoDB, and Python. The app is bombing when trying to import from the bson module. All modules were installed successfully via pip and I'm running in a virtualenv, so I'm not…
Wesley Tansey
  • 4,555
  • 10
  • 42
  • 69
28
votes
2 answers

How to insert datetime string into Mongodb as ISODate using pymongo

How to insert datetime string like this "2017-10-13T10:53:53.000Z" into mongo db as ISODate? I get a string in mongodb when I insert: datetime.strptime("2017-10-13T10:53:53.000Z", "%Y-%m-%dT%H:%M:%S.000Z")
ArchieTiger
  • 2,083
  • 8
  • 30
  • 45
28
votes
1 answer

PyMongo create unique index with 2 or more fields

How can i create index in pymongo with 2 fields, to be unique together? I have this code: self.db[self.mongo_collection].create_index("url", unique=True) but i need to be unique with url and category.
Mirza Delic
  • 4,119
  • 12
  • 55
  • 86
28
votes
3 answers

'ObjectId' object has no attribute 'getTimeStamp'

I'm trying to get the timestamp from an ObjectID, but Mongo keeps giving me this error. Am I missing an import? What's the best way to convert the timestamp to a standard date format? video['date'] = video['_id'].getTimeStamp()
zakdances
  • 22,285
  • 32
  • 102
  • 173
27
votes
3 answers

Make $elemMatch (projection) return all objects that match criteria

I will use the example from here { _id: 1, zipcode: 63109, students: [ { name: "john", school: 102, age: 10 }, { name: "jess", school: 102, age: 11 }, { name: "jeff", school: 108, age: 15 } …
Diolor
  • 13,181
  • 30
  • 111
  • 179
26
votes
1 answer

MongoDB python bindings an order of magnitude slower than java?

I asked this same question on the mongodb-user list: http://groups.google.com/group/mongodb-user/browse_thread/thread/b3470d6a867cd24 I was hoping someone on this forum might have some insight... I've run a simple experiment comparing the…
Sam
  • 261
  • 2
  • 4
26
votes
1 answer

How to drop a collection with pymongo?

I use scarpy to crawl data and save it to cloud hosting mLab successfully with MongoDB. My collection name is recently and data's count is 5. I want to crawl data again and update my collection recently, so i try to drop the collection and then…
Morton
  • 5,380
  • 18
  • 63
  • 118
26
votes
2 answers

Deleting document in PyMongo from id

I seem to be struggling to find the right way of deleting a document. I.e. should I be using remove() or delete_one() for example and also what is the canonical method of deleting by id, which is a string. I.e. should I be using the…
disruptive
  • 5,687
  • 15
  • 71
  • 135
26
votes
7 answers

How to know which version of PyMongo is running on my project

I'm developing a python project, in the requirements file I have three different types of PyMongo Flask-PyMongo==0.3.1 pymongo==2.7 flask-mongoengine==0.7.1 How can I define which version I'm using?
Babel
  • 589
  • 2
  • 9
  • 23
26
votes
3 answers

python bson.errors.InvalidDocument: Cannot encode object: datetime.date(2015, 3, 1)

I have the following functions: # this is in a module called 'dbw_lib' def dateTimeOuput(start_days_back, end_days_back): start_delta = datetime.timedelta(days=start_days_back) end_delta = datetime.timedelta(days=end_days_back) …
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
26
votes
6 answers

Mongodb bulk write error

I'm executing bulk write bulk = new_packets.initialize_ordered_bulk_op() bulk.insert(packet) output = bulk.execute() and getting an error that I interpret to mean that packet is not a dict. However, I do know that it is a dict. What could be the…
David Makovoz
  • 1,766
  • 2
  • 16
  • 27