mongodb-query

Mongodb find nested dict element

Mongodb find nested dict element Question: { "_id" : ObjectId("63920f965d15e98e3d7c450c"), "first_name" : "mymy", "last_activity" : 1669278303.4341061, "username" : null, "dates" : { "29.11.2022" : { }, "30.11.2022" : { } }, "user_id" : "1085116517" } How can I find all documents with 29.11.2022 contained in date? I tried many things but in all of them …

Total answers: 1

Why is find_one() timing out when find() works just fine using MongoDB?

Why is find_one() timing out when find() works just fine using MongoDB? Question: The code below connects to MongoDB, then attempts to find the specified record, which exists. The collections are very small. client = pymongo.MongoClient("PRIVATE_MONGO_ACCESS_HERE?retryWrites=true&w=majority") db = client.my_db score_holder = db[‘app_data’].find_one({‘score_holder’: True}) Result: pymongo.errors.ServerSelectionTimeoutError I’m pretty sure the above code was working fine a …

Total answers: 1

PyMongo – How to query the number of properties in the object

PyMongo – How to query the number of properties in the object Question: I need to retrieve numerical data from hundreds of documents looking like this one: { "_id": "123", "Notes": { "1222": "Something is here" }, "BehavioralData": { "Folder1": { "Sex": "Male", "Age": "22", "Date": "", "ResearchGroup": "", "Institution": "University of Manitoba" }, "MoCA": …

Total answers: 1

Update document if value there is no match

Update document if value there is no match Question: In Mongodb, how do you skip an update if one field of the document exists? To give an example, I have the following document structure, and I’d like to only update it if the link key is not matching. { “_id”: { “$oid”: “56e9978732beb44a2f2ac6ae” }, “domain”: …

Total answers: 3

Append item to MongoDB document array in PyMongo without re-insertion

Append item to MongoDB document array in PyMongo without re-insertion Question: I am using MongoDB as the back-end database for Python web application (PyMongo + Bottle). Users can upload files and optionally ‘tag’ these files during upload. The tags are stored as a list within the document, per below: { “_id” : ObjectId(“561c199e038e42b10956e3fc”), “tags” : …

Total answers: 5

Why does upsert a record using update_one raise ValueError?

Why does upsert a record using update_one raise ValueError? Question: I want to add a record to the collection if the key doesn’t already exist. I understand [MongoDB][1] offers the upsertfor this so I did a db.collection.update({“_id”:”key1″},{“_id”:”key1″},True) This seems to work. However in the Pymongo documentation it says that update is deprecated and use to …

Total answers: 2

How to convert a pymongo.cursor.Cursor into a dict?

How to convert a pymongo.cursor.Cursor into a dict? Question: I am using pymongo to query for all items in a region (actually it is to query for all venues in a region on a map). I used db.command(SON()) before to search in a spherical region, which can return me a dictionary and in the dictionary …

Total answers: 6

How to check if a pymongo cursor has query results

How to check if a pymongo cursor has query results Question: I need to check if a find statement returns a non-empty query. What I was doing was the following: query = collection.find({“string”: field}) if not query: #do something Then I realized that my if statement was never executed because find returns a cursor, either …

Total answers: 4

Query Mongodb on month, day, year… of a datetime

Query Mongodb on month, day, year… of a datetime Question: I’m using mongodb and I store datetime in my database in this way for a date “17-11-2011 18:00” I store: date = datetime.datetime(2011, 11, 17, 18, 0) db.mydatabase.mycollection.insert({“date” : date}) I would like to do a request like that month = 11 db.mydatabase.mycollection.find({“date.month” : month}) …

Total answers: 7

mongodb: insert if not exists

mongodb: insert if not exists Question: Every day, I receive a stock of documents (an update). What I want to do is insert each item that does not already exist. I also want to keep track of the first time I inserted them, and the last time I saw them in an update. I don’t …

Total answers: 10