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
226
votes
46 answers

mongo - couldn't connect to server 127.0.0.1:27017

I am coming from riak and redis where I never had an issue with this services starting, or to interact. This is a pervasive problem with mongo and am rather clueless. Restarting does not help.I am new to mongo. mongo MongoDB shell version:…
Tampa
  • 75,446
  • 119
  • 278
  • 425
213
votes
9 answers

How to sort mongodb with pymongo

I'm trying to use the sort feature when querying my mongoDB, but it is failing. The same query works in the MongoDB console but not here. Code is as follows: import pymongo from pymongo import Connection connection = Connection() db =…
WildBill
  • 9,143
  • 15
  • 63
  • 87
152
votes
4 answers

Performing regex queries with PyMongo

I am trying to perform a regex query using PyMongo against a MongoDB server. The document structure is as follows { "files": [ "File 1", "File 2", "File 3", "File 4" ], "rootFolder": "/Location/Of/Files" } I want to get all…
RC1140
  • 8,423
  • 14
  • 48
  • 71
129
votes
1 answer

Using .sort with PyMongo

With PyMongo, when I try to retrieve objects sorted by their 'number' and 'date' fields like this: db.test.find({"number": {"$gt": 1}}).sort({"number": 1, "date": -1}) I get this error: TypeError: if no direction is specified, key_or_list must be…
KennyPowers
  • 4,925
  • 8
  • 36
  • 51
126
votes
15 answers

How to import data from mongodb to pandas?

I have a large amount of data in a collection in mongodb which I need to analyze. How do i import that data to pandas? I am new to pandas and numpy. EDIT: The mongodb collection contains sensor values tagged with date and time. The sensor values are…
Nithin
  • 2,223
  • 4
  • 21
  • 29
121
votes
2 answers

How can I use 'Not Like' operator in MongoDB

I can use the SQL Like Operator using pymongo, db.test.find({'c':{'$regex':'ttt'}}) But how can I use Not Like Operator? I tried db.test.find({'c':{'$not':{'$regex':'ttt'}}) but got error: OperationFailure: $not cannot have a regex
KyungHoon Kim
  • 2,859
  • 2
  • 23
  • 26
111
votes
1 answer

search by ObjectId in mongodb with pymongo

I need to search an ObjectId with python using pymongo but I always get this error. Any ideas how to search? import pymongo from pymongo import MongoClient from pymongo import ObjectId gate = collection.find({'_id':…
Gerswin Lee
  • 1,265
  • 2
  • 11
  • 12
103
votes
6 answers

JSON ValueError: Expecting property name: line 1 column 2 (char 1)

I am having trouble using json.loads to convert to a dict object and I can't figure out what I'm doing wrong.The exact error I get running this is ValueError: Expecting property name: line 1 column 2 (char 1) Here is my code: from kafka.client…
dredbound
  • 1,579
  • 3
  • 17
  • 27
98
votes
7 answers

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

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 there is a…
gladys0313
  • 2,569
  • 6
  • 27
  • 51
95
votes
5 answers

How do I update a Mongo document after inserting it?

Let's say I insert the document. post = { some dictionary } mongo_id = mycollection.insert(post) Now, let's say I want to add a field and update it. How do I do that? This doesn't seem to work..... post = mycollection.find_one({"_id":mongo_id})…
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
90
votes
7 answers

How to find names of all collections using PyMongo?

How to find names of all collections using PyMongo and find all fields in chosen collection ? I have name of database and name of chosen collection. (Scenario : user input name of database, need to find all collections and show in dropdown list,…
Damir
  • 54,277
  • 94
  • 246
  • 365
90
votes
4 answers

Get all documents of a collection using Pymongo

I want to write a function to return all the documents contained in mycollection in mongodb from pymongo import MongoClient if __name__ == '__main__': client = MongoClient("localhost", 27017, maxPoolSize=50) db=client.mydatabase …
MAYA
  • 1,243
  • 1
  • 12
  • 20
87
votes
7 answers

pymongo.errors.CursorNotFound: cursor id '...' not valid at server

I am trying to fetch some ids that exist in a mongo database with the following code: client = MongoClient('xx.xx.xx.xx', xxx) db = client.test_database db = client['...'] collection = db.test_collection collection = db["..."] for cursor in…
snake plissken
  • 2,649
  • 10
  • 43
  • 64
87
votes
3 answers

How to close a mongodb python connection?

I'm doing a python script that writes some data to a mongodb. I need to close the connection and free some resources, when finishing. How is that done in Python?
lrente
  • 1,070
  • 1
  • 9
  • 27
87
votes
2 answers

Can you specify a key for $addToSet in Mongo?

I have a document: { 'profile_set' : [ { 'name' : 'nick', 'options' : 0 }, { 'name' : 'joe', 'options' : 2 }, { 'name' : 'burt', 'options' : 1 } ] } and would like to add a new document to the profile_set set if the name doesn't…
nickponline
  • 25,354
  • 32
  • 99
  • 167
1
2 3
99 100