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
39
votes
6 answers

MongoDB return True if document exists

I want to return true if a userID already exists and false otherwise from my collection.I have this function but it always returns True. def alreadyExists(newID): if db.mycollection.find({'UserIDS': { "$in": newID}}): return True …
user94628
  • 3,641
  • 17
  • 51
  • 88
38
votes
1 answer

AutoReconnect exception "master has changed"

Having some trouble understanding the right approach here. I have a connection to a mongodb replica set with three members (standard master-slave-slave). Everything is working fine with the connection when the master remains…
DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77
38
votes
3 answers

Removing _id element from Pymongo results

I'm attempting to create a web service using MongoDB and Flask (using the pymongo driver). A query to the database returns documents with the "_id" field included, of course. I don't want to send this to the client, so how do I remove it? Here's a…
ddw
  • 391
  • 1
  • 3
  • 5
37
votes
3 answers

How to delete a MongoDB collection in PyMongo

How to check in PyMongo if collection exists and if exists empty (remove all from collection)? I have tried like collection.remove() or collection.remove({}) but it doesn't delete collection. How to do that ?
Damir
  • 54,277
  • 94
  • 246
  • 365
37
votes
4 answers

PyMongo vs MongoEngine for Django

For one of my projects I prefered using Django+Mongo. Why should I use MongoEngine, but not just PyMongo? What are advantages? Querying with PyMongo gives results that are allready objects, aren't they? So what is the purpose of MongoEngine?
megido
  • 4,135
  • 6
  • 29
  • 33
37
votes
4 answers

Getting Spark, Python, and MongoDB to work together

I'm having difficulty getting these components to knit together properly. I have Spark installed and working successfully, I can run jobs locally, standalone, and also via YARN. I have followed the steps advised (to the best of my knowledge) here…
Philip O'Brien
  • 4,146
  • 10
  • 46
  • 96
37
votes
5 answers

How can I load data from mongodb collection into pandas' DataFrame?

I am new to pandas (well, to all things "programming"...), but have been encouraged to give it a try. I have a mongodb database - "test" - with a collection called "tweets". I access the database in ipython: import sys import pymongo from pymongo…
user2161725
  • 667
  • 2
  • 7
  • 12
35
votes
5 answers

What is the correct way to query MongoDB for _id using string by using Python?

I am using pymongo driver. Supposedly, one can use a string to query the _id field of a document, like this: thing = db.things.find_one({'_id':'4ea113d6b684853c8e000001'}) But it doesn't work. What am I doing wrong?
MFB
  • 19,017
  • 27
  • 72
  • 118
35
votes
8 answers

How to mock mongodb for python unittests?

I am using mock module for Python 2.7 to mock my other functions and using unittest for writing unit tests. I am wondering if mocking the MongoDB is different than using mock functionality (mock.patch a function that is being called?) Or I need to…
Mahshid Zeinaly
  • 3,590
  • 6
  • 25
  • 32
35
votes
5 answers

JSON serializing Mongodb

I am using the python package pymongo to retrieve data from a mongodb database. >>> r = collection.find() # returns an object of class 'Cursor' Then I convert to a list >>> l = list(r) # returns a 'list' of 'dict' here is what…
RockScience
  • 17,932
  • 26
  • 89
  • 125
33
votes
1 answer

Flask - Bad Request The browser (or proxy) sent a request that this server could not understand

I'm trying to do a file upload & enter data task into my MongoDB using flask but I had this error when I filled my form & upload the image: Bad Request The browser (or proxy) sent a request that this server could not understand. my HTML code …
Roxas Zohbi
  • 599
  • 4
  • 13
  • 20
33
votes
5 answers

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

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" :…
deadbits
  • 377
  • 1
  • 3
  • 8
33
votes
4 answers

How to make a query date in mongodb using pymongo?

I'm trying to perform a query date in mongodb, but the result is always empty. My query is as follows: //in the begin code def __init__(self): self.now = datetime.now() self.db = conexaoMongo() self.horaInicio = self.now -…
carlaodev
  • 621
  • 2
  • 8
  • 18
32
votes
3 answers

Is it possible to use Variable for collection name using pymongo?

Is it possible to use Variable for collection name using pymongo? for example: col = 'my_collection' db.col.update()
ehsan shirzadi
  • 4,709
  • 16
  • 69
  • 112
32
votes
2 answers

pymongo : delete records elegantly

Here is my code to delete a bunch of records using pymongo ids = [] with MongoClient(MONGODB_HOST) as connection: db = connection[MONGODB_NAME] collection = db[MONGODN_COLLECTION] for obj in collection.find({"date": {"$gt":…
icn
  • 17,126
  • 39
  • 105
  • 141