6,880 questions
3
votes
1
answer
181
views
MongoDB/Pymongo giving random ServerSelectionTimeoutError messages
We're using Tornado-based services, which fork and then create DB connections. This was all working fine until we tried upgrading to python 3.11 (from 3.8) and using pymongo 4.8.0. Now, we very ...
0
votes
0
answers
60
views
MongoDB Atlas Timeout After Deployment Update — "ReplicaSetNoPrimary"
I updated my backend deployment (Flask app) with a new feature not related to database logic, and suddenly MongoDB Atlas connections no longer work. Before the update, everything was fine.
Local ...
0
votes
0
answers
61
views
pymongo is not inserting document with base64 string
I have data which pymongo fails to upload, and, I cannot understand why.
Data available here
main2.py:
from pymongo import MongoClient
import json
client = MongoClient(host='localhost')
db = client....
1
vote
0
answers
91
views
How can I programmatically determine if a PyMongo command is a read or write for permission logic (without a hardcoded command list)?
I'm building an authorization/access control system in a Python application that uses PyMongo to access MongoDB. I need to dynamically decide—at runtime and for arbitrary query shapes—whether a given ...
1
vote
1
answer
94
views
PyMongo MongoClient - is there a way to see number of open connections?
The docs say that each mongo client maintains a connection pool, the max size of which I can specify. Is there anyway to check the number of open connections made by a specific mongo client at a given ...
1
vote
1
answer
264
views
Pymongo and Beanie Incompatibility issues
I'm trying to migrate from motor to pymongo's AsyncMongoClient.
After doing some upgrading/installing on pymongo I am having the below error when running this import from beanie import Document
...
0
votes
0
answers
46
views
How to set timestamp on document update only when it's modified with MongoDB (pymongo)?
Let's say I have a command:
my_collection.update_one({...}, {
"$set": { "title": "Title 1" }
})
How to make it adding update_at field with current date when update ...
1
vote
3
answers
95
views
MongoDB count_documents() returns 19 but find() returns empty list - Replica Set Consistency Issue
I'm experiencing a consistency issue with MongoDB in production. I have a 3-node replica set deployed on OVH, and sometimes count_documents() and find() return different results for the exact same ...
0
votes
0
answers
73
views
Static typing for database schema models that handle foreign keys/dbrefs as `external_document: ExternalDocumentModel | DbRef`
We're using Python (3.12) with Pydantic models to represent schemas for our MongoDB collections, which we then instantiate with SomeModel.model_validate(<results of pymongo query>). We define ...
0
votes
1
answer
47
views
mongo add new properties instead of overwriting existing ones if using dot notation and arrays
i have a mongodb with documents containing a list of "pages", as sub-documents,
like
{
pages: [
{ a:1, b: "foo"},
{ a:2, b: "bar"},
]
}
now i want to update the ...
0
votes
1
answer
96
views
mongod db.killAllSessions() reports that it works, but doesn't kill the sessions
I'm very new to MongoDB and I'm trying to get the killAllSessions or one of its variants to work, in mongosh and/or using pymongo. I have a simple, standalone instance of mongod running in a Docker ...
0
votes
1
answer
94
views
MongoDB: rename a field with dot in it
I have a MongoDB collection that contains several fields with dots, like:
{
"symbol.name": "Some name"
"symbol.state": "Some state"
// etc.
}
Now, ...
0
votes
1
answer
48
views
Requesting clarity on PyMongo's maxIdleTimeMS parameter
per the official PyMongo's documentions for maxIdleTimeMS
maxIdleTimeMS (optional): The maximum number of milliseconds that a connection can remain
idle in the pool before being removed and ...
1
vote
1
answer
55
views
PyMongo - Group by year based on subdocument date
I have a MongoDB document like:
[
{
_id: ObjectId('67cfd69ba3e561d35ee57f51'),
created_at: ISODate('2025-03-11T06:22:19.044Z'),
conversation: [
{
id: '...
0
votes
1
answer
60
views
Update an array value in document with Pymongo
I am creating a tracker that tracks the usage of invites on discord and who used which invite when a member joins. I am storing the data in mongodb. I am having trouble update the uses value which I ...
1
vote
0
answers
98
views
Get MongoDB telemetry using Open Telemetry auto instrumentation for flask?
My company runs a Flask API connected to MongoDB on atlas. Currently, we have minimal logging and metrics and rely solely on Altas to monitor DB usage. This month we realized the DB is being queried ...
0
votes
0
answers
35
views
Jupyter_dash not loading a dash
I'm doing a project for school that uses an .ipynb file to create a dashboard and connect to a MongoDB collection to output a table and map. The code is here:
# Setup the Jupyter version of Dash
from ...
2
votes
1
answer
385
views
mongomock BulkOperationBuilder.add_update() unexpected keyword argument 'sort'
I'm testing a function that performs a bulk upsert using UpdateOne with bulk_write. In production (using the real MongoDB client) everything works fine, but when running tests with mongomock I get ...
0
votes
0
answers
54
views
Consistent DateTime datatype when using both PyMongo and flask mongoengine
I am trying to maintain a consistent datatype for my datetimes in a Mongo collection, where some come from PyMongo, and others come from flask-mongoengine.
In Flask, the relevant model entry is:
...
0
votes
1
answer
48
views
Pymongo, GridFS, ThreadPoolExecutor, download multiple files simultaneously
I need to download multiple files at once from MongoDB, and then process the result as soon as file is downloaded. But the thing is, it's not faster of sync cycle. It seems like it still waits for ...
0
votes
0
answers
127
views
mock motor AsyncIOMotorClient with uuid settings
I have AsyncIOMotorClient code as below.
def get_client():
"""
Get Client.
Get the client for the database connection.
"""
return motor.motor_asyncio....
1
vote
1
answer
626
views
PyMongo Async client not raising exception when connection fails
It seems that a pymongo 4.10 async client does not raise an exception when there is a problem with the connection.
Taken from the doc, a test without any mongo DB running locally yields:
>>> ...
0
votes
0
answers
36
views
Pandas does not save dtype int64 as long in mongodb
One of the columns in a oracle table has Integer values which needs to be stored in mongodb as Long. Currently this column has values less than int32 can store. But as a futureproofing we want it to ...
1
vote
2
answers
231
views
FastAPI routers do not detect initialized collections using beanie
I have a big application in Python 3.11, but I've narrowed the problem into a smaller example to demonstrate, and I get the same error.
Assuming I have a class called Product which is a Document:
# /...
0
votes
0
answers
49
views
Is it possible to make arrays equal after update_many in mongodb?
I have mongodb base structure
[{"_Id": 12345678910, "offer_id": [1,2,3,4], "barcodes": [200, 300, 400, 500], "groups":[]"}, {"_Id": 12345678911, &...
0
votes
1
answer
39
views
pymongo throws WinError 10048 when using multiprocessing
I am using pymongo in multiprocessing. I use 6 processes to calculate something and insert the calculation result into MongoDB.
import multiprocessing
import sys
import pymongo
import datetime
from ...
0
votes
0
answers
53
views
PyMongo: convertation BSON to Dict not working
my code here:
import os
import bson
from bson.codec_options import CodecOptions
bson_codec_options = CodecOptions(
datetime_conversion='DATETIME_AUTO',
tz_aware=True,
...
1
vote
0
answers
219
views
FastAPI - pymongo + sync routes seemingly MUCH faster than motor + async routes
I am using FastAPI + pymongo (sync routes) and saw motor was recommended for 'best practices' on FastAPI.
I set up two routes with the same DB call, one using async router + motor, one using sync + ...
0
votes
2
answers
306
views
PyMongo on Azure Cosmos and MongoDB
I created an Azure Cosmos (RU) Serverless database (sharded) with a MongoDB engine and would like to use Python and pymongo to run queries on it. I am not managing to connect to it in any way.
I have ...
0
votes
2
answers
61
views
Pymongo - fetch documents by multiple tags
I need to fetch documents given a list of tags, but I am having trouble finding the right syntax. For example, I have this collection:
{
"name": "Mike",
"roll_no"...
0
votes
1
answer
121
views
Pymongo query with dates returns no results
I have the following python 3.12.4 code that inserts a JSON in a mongoDB deployed locally through a docker container using dockerhub' image "mongo:4.4.7"
myClient = pymongo.MongoClient(&...
2
votes
1
answer
57
views
Aggregation pipeline MongoDB to avoid multiple queries
I have post documents like this :
post = {
"_id": "uid1",
"userID": "user1",
"likeNum": 30,
}
My current pipeline looks like this with user_ids ...
0
votes
1
answer
49
views
Consistently perform concurrent write operations in REST API that uses MongoDB
I'm writing a REST API using Flask and MongoDB (I use pymongo to interact with the database). I'm having an issue that seems to be related to race conditions. I have been able to reproduce my use case ...
1
vote
0
answers
44
views
Pymongo push operation overriding in Python
I am trying to do the following operation concurrently across many machine
operation = {"$push": {field_name: task}}
result = table.find_one_and_update({'_id': _id}, operation, ...
0
votes
1
answer
100
views
How to Reuse MongoClient Connections Efficiently in Django with PyMongo?
I'm working on a project using Django version 3.0.8 and PyMongo version 3.11.3. Currently, for each incoming request, a new MongoClient is created, and the connection is destroyed once the request is ...
0
votes
0
answers
86
views
Unable to connect to single-node mongodb replica set with pymongo in docker compose
The task is to set up a MongoDB replica set from a single node and connect to it from another container using pymongo.
After running docker compose up -d, the behavior is as follows:
The connection to ...
1
vote
1
answer
441
views
How to prevent race conditions with upsert=True in MongoDB when avoiding conflicting entries?
I'm working on a Python app that uses MongoDB (with pymongo for connection) and need to avoid race conditions when adding documents to a collection. Unfortunately, I can't use transactions because I ...
0
votes
0
answers
780
views
pymongo.errors.ConfigurationError: The resolution lifetime expired after 21.215 seconds
When I'm trying to connect to pymongo getting below error
pymongo.errors.ConfigurationError: The resolution lifetime expired after 21.215 seconds: Server 192.168.1.1 UDP port 53 answered The DNS ...
0
votes
1
answer
454
views
MongoDb schema validation error when schema specified ['string'] as bson type of attribute
I have a peice of python code which is using the PyMongo library to insert into my mongodb Collection hosted on MongoDB Atlas.
Im trying to insert the following doc into a collection called Customer ...
1
vote
1
answer
69
views
How to use `pymongo` with `flask` inside of Vercel
I want to add database functionalities to my Flask app hosted on Vercel.
When using pymongo the website throws HTTP 500 saying that my app crashed.
It also says that I should check the logs.
The logs ...
2
votes
2
answers
99
views
Write a single query from multiple queries in MongoDB
I have 2 collections users and files. A user document looks like this :
{
"_id": "user1",
"name": "John"
}
A file document looks like this :
{
"_id&...
1
vote
1
answer
66
views
PyMongo query function with multiple args and passing results to Dash app
Ok, I have a MongoDB database, using a Python module to perform CRUD functions, and passing the resulting info into a Dash app.
Here is the relevant Python method:
def read(self, query):
...
0
votes
1
answer
110
views
Pymongo: create_collection error after updating python to 3.12.5
I'm using Mongo db from Python.
Creating collections using this code:
self.client = MongoClient(MONGODB_CONNECT)
self.db = self.client[MONGODB_NAME]
self.db.create_collection(collection_name, options)
...
0
votes
1
answer
313
views
How to connect to MongoDB using PySpark?
I want to connect to a MongoDb database using PySaprk. I have downloaded mongo-spark-connector_2.12-10.1.0-all.jar and copied it to the Spark jars directory.
This is my code.
from pyspark.sql import ...
0
votes
1
answer
59
views
Python Pandas: Dataframe to_json not index oriented
The default Dataframe.to_json() exports like this:
{
"date": {
"0": Example_date1,
"1": Example_date2
}
"name": {
"0&...
0
votes
1
answer
220
views
How to properly type PyMongo?
I'm trying to use PyMongo in a type safe(ish) way in VS Code. I've tried following the type hints section in the PyMongo documentation and arrived at the following (app is a Flask instance):
from ...
1
vote
1
answer
44
views
MongoDB Grouping and counting by composite field
Here's what my records look like:
I have obtained this by using collection.find().limit(1)
[
{
"_id": {"$oid": "..."},
"husband.firstName": "John&...
0
votes
1
answer
50
views
SSL handshake failed when connecting to Mongodb Atlas from ECS Fargate
An error is output when connecting from ECS Fargate to Mongodb Atlas.
error occured: SSL handshake failed: ac-xvafg1z-shard-00-00.pizmgb2.mongodb.net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 ...
0
votes
0
answers
68
views
How can I get distinct array fields efficiently?
My collection with fields:
[
{
"_id": xxx,
"field": {
"subfield": [
{
"k1": "a",
"k2": "b&...
1
vote
1
answer
44
views
Create array of elements based on another existing array using MongoDB Aggregation
I need to take a field from a document as input, like below:
"participants": ["John", "Peter"],
and make the output field like that:
"participants": [
{
...