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
85
votes
7 answers

How to convert objectid to string

I want to get the string character from an ObjectId object. I use pymongo. eg: ObjectId("543b591d91b9e510a06a42e2"), I want to get "543b591d91b9e510a06a42e2". I see the doc, It says ObjectId.toString(), ObjectId.valueOf(). So I make this code: from…
Simon
  • 1,033
  • 1
  • 8
  • 16
80
votes
4 answers

How to update values using pymongo?

I've a mongodb collection in this form: {id=ObjectId(....),key={dictionary of values}} where dictionary of values is {'a':'1','b':'2'.....} Let dictionary of values be 'd'. I need to update the values of the key in the 'd'. i.e I want to change…
gizgok
  • 7,303
  • 21
  • 79
  • 124
78
votes
3 answers

PyMongo upsert throws "upsert must be an instance of bool" error

I'm running an update on my MongoDB from Python. I have this line: self.word_counts[source].update({'date':posttime},{"$inc" : words},{'upsert':True}) But it throws this error: raise TypeError("upsert must be an instance of bool") But True looks…
75
votes
11 answers

pymongo - "dnspython" module must be installed to use mongodb+srv:// URIs

I am trying to connect MongoDB from Atlas. My mongo uri is: mongodb+srv://abc:123@something.something.com/admin?retryWrites=True My pymongo version is 3.6.1 I have installed dnspython and done import dns But i still get this error: dnspython module…
addicted
  • 2,901
  • 3
  • 28
  • 49
70
votes
4 answers

Collection object is not callable error with PyMongo

Following along the PyMongo tutorial and am getting an error when calling the insert_one method on a collection. In [1]: import pymongo In [2]: from pymongo import MongoClient In [3]: client = MongoClient() In [4]: db = client.new_db In [5]:…
Jason Strimpel
  • 14,670
  • 21
  • 76
  • 106
69
votes
3 answers

how to issue a "show dbs" from pymongo

I'm using pymongo and I can't figure out how to execute the mongodb interactive shell equivalent of "show dbs".
jacobra
  • 2,102
  • 2
  • 14
  • 20
68
votes
1 answer

How to make pymongo's find() return a list?

Pymongo returns a cursor with it I am able to iterate over the results and append their documents to a list. Is there a way to get the result documents in a list directly? Thanks
user971956
  • 3,088
  • 7
  • 30
  • 47
67
votes
7 answers

How do you check if the client for a MongoDB instance is valid?

In particular, I am currently trying to check if a connection to a client is valid using the following function: def mongodb_connect(client_uri): try: return pymongo.MongoClient(client_uri) except pymongo.errors.ConnectionFailure: …
Leeren
  • 1,931
  • 2
  • 20
  • 31
67
votes
6 answers

In Flask convert form POST object into a representation suitable for mongodb

I am using Flask and MongoDB. I am trying to convert the content of request.form into something suitable for saving via PyMongo. It seems like something that should come up often enough to have a ready-made solution. So what Flask give me is…
Ivan P
  • 1,920
  • 2
  • 15
  • 19
65
votes
4 answers

How do I create a new database in MongoDB using PyMongo?

Can I create a new database simply by connecting to the MongoDB server, or is there another way to create it using Python? If so, how is this done?
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
63
votes
4 answers

Test empty string in mongodb and pymongo

Here is my data structure. [{ "name": "David", "lastname": "", }, { "name": "Angela" }] "lastname" is sometimes present and sometimes not and sometime is "". I want to get all rows that have lastname not equal to "". But this does not work. It…
David Dehghan
  • 22,159
  • 10
  • 107
  • 95
62
votes
11 answers

Insert a Pandas Dataframe into mongodb using PyMongo

What is the quickest way to insert a pandas DataFrame into mongodb using PyMongo? Attempts db.myCollection.insert(df.to_dict()) gave an error InvalidDocument: documents must have only string keys, the key was Timestamp('2013-11-23 13:31:00',…
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
61
votes
8 answers

How to get the object id in PyMongo after an insert?

I'm doing a simple insert into Mongo... db.notes.insert({ title: "title", details: "note details"}) After the note document is inserted, I need to get the object id immediately. The result that comes back from the insert has some basic info…
user1125472
  • 611
  • 1
  • 5
  • 3
58
votes
8 answers

In MongoDB's pymongo, how do I do a count()?

for post in db.datasets.find({"test_set":"abc"}).sort("abc",pymongo.DESCENDING).skip((page-1)*num).limit(num): How do I get the count()?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
57
votes
12 answers

Pymongo keeps refusing the connection at 27017

I am trying to run a simple connection to pymongo but it keeps returning that the connection was refused Here is what I tried: >>>from pymongo import Connection >>>connection = Connection('localhost',27017) here is what I get Traceback (most recent…
Ulmer
  • 1,530
  • 2
  • 17
  • 24