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

Pymongo / MongoDB: create index or ensure index?

I don't understand the difference between create_index and ensure_index in pymongo. On the MongoDB indexes page, it says you can create an index by calling the ensureIndex() However in pymongo there are two different commands create_index and…
YXD
  • 31,741
  • 15
  • 75
  • 115
56
votes
6 answers

Fast or Bulk Upsert in pymongo

How can I do a bulk upsert in pymongo? I want to Update a bunch of entries and doing them one at a time is very slow. The answer to an almost identical question is here: Bulk update/upsert in MongoDB? The accepted answer doesn't actually answer the…
54
votes
7 answers

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

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 =…
kschaeffler
  • 4,083
  • 7
  • 33
  • 41
50
votes
5 answers

How to check if a pymongo cursor has query results

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…
Alberto Coletta
  • 1,563
  • 2
  • 15
  • 24
48
votes
2 answers

Why does upsert a record using update_one raise ValueError?

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…
johhny B
  • 1,342
  • 1
  • 18
  • 37
45
votes
2 answers

How to limit mongo query in python

I am trying to retrieve data from mongodb with python. My db contains lots of data. So I want to limit the data while retrieving. I tried import datetime from pymongo import Connection connection = Connection('localhost',27017) db =…
Mulagala
  • 8,231
  • 11
  • 29
  • 48
45
votes
5 answers

Remove multiple documents from mongo in a single query

I have a list of mongo '_id' which I want to delete. Currently I am doing this # inactive_users --> list of inactive users for item in inactive_users: db.users.remove({'_id' : item}) but my problem is the list is too huge... (it might go…
Anurag Sharma
  • 4,839
  • 13
  • 59
  • 101
43
votes
3 answers

pymongo- How can I have distinct values for a field along with other query parameters

I am using pymongo and want to have distinct values for a field such that I can also pass other query parameters. For example, I have entries like: { id = "my_id1" tags: [tag1, tag2, tag3], category: "movie", } { id = "my_id2" tags:…
Sushant Gupta
  • 8,980
  • 5
  • 43
  • 48
41
votes
5 answers

Create an ISODate with pyMongo

I've been trying to find a way to create an ISODate object whith pyMongo client, but without any success so far. I use http://pypi.python.org/pypi/pymongo3 client, which is the only serious one available in Python 3 for now, but the problem doesn't…
Guillaume Lebourgeois
  • 3,796
  • 1
  • 20
  • 23
41
votes
5 answers

pymongo auth failed in python script

I have installed mongodb and enabled auth. and its working find. I can connect it from remote notebook using robomongo application: Host: SERVER_IP PORT: 27017 DATEBASE: prod-db USERNAME: user_name PASS: user_password Auth Mechanism: MONGODB-CR and…
sharafjaffri
  • 2,134
  • 3
  • 30
  • 47
41
votes
11 answers

How to get the length of a cursor from mongodb using python?

I'm looking for a feasible way to get the length of cursor got from MongoDB.
QIAN KEQIAO
  • 541
  • 1
  • 4
  • 7
41
votes
2 answers

How do I drop a MongoDB database using PyMongo?

I want to drop a database in MongoDB similarly to use db.dropDatabase() in the Mongo shell. How do I do that in PyMongo?
qff
  • 5,524
  • 3
  • 37
  • 62
41
votes
4 answers

How to hide _id from Aggregation?

I've this query: produits = yield motor.Op(db.users.aggregate, [{"$unwind":"$pup"},{"$match":{"pup.spec.np":nomp}}, {"$group":{"_id":"$pup.spec.id","pup":{"$push":"$pup"}}}]) which gives me this result: print produits {u'ok': 1.0, u'result':…
Abdelouahab Pp
  • 4,252
  • 11
  • 42
  • 65
40
votes
13 answers

ImportError: No module named 'pymongo'

I have a problem running pymongo on Win 7 (64) with Python 3.4, mongodb 4.2.10. The error output is as follows: import pymongo ImportError: No module named 'pymongo' The code is pretty simple: import pymongo from pymongo import…
user3151858
  • 790
  • 4
  • 13
  • 26
39
votes
1 answer

How can I create an index with pymongo

I want to enable text-search at a specific field in my Mongo DB. I want to implement this search in python (-> pymongo). When I follow the instructions given in the internet: db.foo.ensure_index(('field_i_want_to_index', 'text'),…
Maximilian
  • 1,325
  • 2
  • 14
  • 35