Questions tagged [pymongo-3.x]

Version 3 of [pymongo]

Version 3 of the official Python driver for MongoDB.

PyMongo 3 is not backward compatible with PyMongo 2:

PyMongo 3.0 brings a large number of API changes. Be sure to read the changes listed below before upgrading from PyMongo 2.x

Warning: PyMongo no longer supports Python 2.4, 2.5, or 3.1. If you must use PyMongo with these versions of Python the 2.x branch of PyMongo will be minimally supported for some time.

See the full changelog for more information.

If you're not sure what version of PyMongo you're running, consider using the generic tag.

272 questions
54
votes
23 answers

Why is PyMongo 3 giving ServerSelectionTimeoutError?

I'm using: Python 3.4.2 PyMongo 3.0.2 mongolab running mongod 2.6.9 uWSGI 2.0.10 CherryPy 3.7.0 nginx 1.6.2 uWSGI start params: --socket 127.0.0.1:8081 --daemonize --enable-threads --threads 2 --processes 2 I setup my MongoClient ONE…
drfence
  • 1,287
  • 2
  • 15
  • 29
23
votes
3 answers

How to escape @ in a password in pymongo connection?

My question is a specification of how can i validate username password for mongodb authentication through pymongo?. I'm trying to connect to a MongoDB instance using PyMongo 3.2.2 and a URL that contains the user and password, as explained in…
gmauch
  • 1,079
  • 4
  • 25
  • 35
22
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
  • 9,100
  • 24
  • 78
  • 115
13
votes
1 answer

How to count pymongo aggregation cursor without iterating

I want to get the total number of records in an aggregate cursor in pymongo version 3.0+. Is there any way to get total count without iterating over the cursor? cursor = db.collection.aggregate([{"$match": options},{"$group": {"_id":…
jadhavmahendra7
  • 131
  • 1
  • 5
12
votes
1 answer

Pymongo $in Query Not Working

Seeing some strange behavior in Pymongo $in query. Looking for records that meet the following query: speciesCollection.find({"SPCOMNAME":{"$in":['paddlefish','lake sturgeon']}}) The query returns no records. If I change it to find_one the it…
Jer
  • 123
  • 1
  • 1
  • 6
10
votes
2 answers

Why is PyMongo count_documents is slower than count?

In db['TF'] I have about 60 million records. I need to get the quantity of the records. If I run db['TF'].count(), it returns at once. If I run db['TF'].count_documents({}), that is a such long time before I get the result. However, the count method…
Threegirl
  • 103
  • 1
  • 1
  • 4
10
votes
2 answers

PyMongo find query returns empty/partial cursor when running in a Django+uWsgi project

We developed a REST API using Django & mongoDB (PyMongo driver). The problem is that, on some requests to the API endpoints, PyMongo cursor returns a partial response which contains less documents than it should (but it’s a completely valid JSON…
Shahinism
  • 1,155
  • 2
  • 16
  • 30
9
votes
3 answers

Bulk update in Pymongo using multiple ObjectId

I want to update thousands of documents in mongo collection. I want to find them using ObjectId and then whichever document matches , should be updated. My update is same for all documents. I have list of ObjectId. For every ObjectId in list, mongo…
Pratik Gujarathi
  • 863
  • 9
  • 18
8
votes
1 answer

insert_many with upsert - PyMongo

I have some data like this: data = [{'_id': 1, 'val': 5}, {'_id': 2, 'val': 1}}] current data in db: >>> db.collection.find_one() {'_id': 1, 'val': 3} I always receive unique rows but am not sure if any of them already exists in DB…
Kevad
  • 2,109
  • 15
  • 25
7
votes
4 answers

How to dump a collection to json file using pymongo

I am trying to dump a collection to .json file but after looking in pymongo tutorial I can not find any thing that relates to it. Tutorial link: https://api.mongodb.com/python/current/tutorial.html
AnhNg
  • 89
  • 1
  • 1
  • 6
6
votes
1 answer

Python pyodbc.row to list

I am trying to create a python script to convert from SQLServer database to Mongodb. In the query from SQLServer, I am returning the data as JSON using the FOR JSON AUTO method. When I use the following code, I cannot get the right type for my var…
Heath
  • 123
  • 2
  • 11
6
votes
1 answer

How to cleanly shutdown Change Streams with Motor?

TL; DR This was indeed a bug in Motor 1.2.0 that was promptly fixed by A. Jesse Jiryu Davis and is available in version 1.2.1 or greater of the driver. Original Question I wrote a program to monitor changes to a MongoDB collection using its new…
Rafa
  • 8,431
  • 4
  • 35
  • 58
6
votes
1 answer

Pymongo's update_one() returns UpdateResult with AttributeError

I just upgraded my MongoDB and Pymongo to the latest version 3.2.1 to be able to use the UpdateResult object after calling update_one(). But for some reason the returned object has one field that contains an AttributeError. Take a look at my Pycharm…
BeePi
  • 163
  • 1
  • 8
6
votes
1 answer

Improve PyMongo socket recv

I have done some profiling in my code (using PyMongo 3.0.3 with a lot of bulk operations) with cProfile and it turns out that a lot of time is spent on sockets: 14006 68.823 0.005 68.823 0.005 {method 'recv' of '_socket.socket' objects} I…
cenouro
  • 695
  • 3
  • 15
5
votes
1 answer

Flask PyMongo string back to ObjectID

I am using flask with pymongo and I have a case where my Object Id is being converted into a string. How can I change it back to an Object Id so I can use if for querying ? From : 59d7ef576cab3d6118805a20 type is To:…
Max Powers
  • 845
  • 2
  • 19
  • 42
1
2 3
18 19