Skip to content

Tag: pymongo

How can I limit output of MongoDB query in Python?

I have this example MongoDB entry in a users DB: And I’m using Pymongo to query the file named “test”: users.find_one({‘files.filename’:”test”}) How can I limit the output from Pymongo, so it only gives me the dict where “test” is contained? In other words, I want it to output this: Answer Here’s one way you could do it. Try it on

How to sort by year using a date string in PyMongo Aggregate

Me and another programmer have run around in circles on this and cannot seem to find an answer online. We have a MongoDB collection into which we have inserted documents which have been pulled from an API. Each document has a field called attributes.created which is a string. The format of the string is like this: “2019-04-25T07:06:02Z” Our aim is

Aggregation $match within a $sum

I was wondering if it was possible to somehow use the $match operator within the $sum function for aggregation. I want to be able to use the value of the $sum operator within the project fields somehow, I just don’t really understand what the right approach would be for this. Sample Input (may be too long): Sample Output: (

How to construct pymongo query string from dictionary

My MongoDB documents looks like {‘k1’: ‘v1’, ‘k2’: ‘v2’, ‘k3’: ‘v3’ . . .} Would like to construct a pymongo query string where Keys and Values in maDict are Match, gtDict are greater than and ltDict are less than and display only the fields specified in diDict: I could think of converting each dictionary to construct the query string. Is

“Update” _id in mongodb document

PS: I only have to do this due to business requirements I’m able to achieve it using mongosh, but since there are multiple records to be updated, I’m trying to implement a simple python script to automate the task. Is it possible to do this with pymongodb? I’m not able to set the new Id in the doc variable in

How to wait pymongo find() results without looping all rows?

Not getting any result in pymongo by find() function in my collection: But if I loop that variable, then it loads: The only thing I did is adding a loop (see line ): So if I put that loop it will load all rows and I will see retrieved: 46 How can I wait it to return results without looping

Perform $gte and $lt on the same field _id in MongoDB

db.comments.find({“_id” : {“$gte”: ObjectId(“6225f932a7bce76715a9f3bd”), “$lt”:ObjectId(“6225f932a7bce76715a9f3bd”)}}).sort({“created_datetime”:1}).limit(10).pretty() I am using this query which should give me the current “6225f932a7bce76715a9f3bd” doc, 4 docs inserted before this and 5 docs inserted after this. But currently when i run this query, i get null result. Where am i going wrong ?? Answer I had no other option but to seperate my queries in order to