Questions tagged [pymongo-2.x]

Version 2 of [pymongo]

Version 2 of the official Python driver for MongoDB.

PyMongo 3 is not backward compatible with PyMongo 2 (see the changelog). If you're not sure what version of PyMongo you're running, consider using the generic tag.

20 questions
3
votes
1 answer

Using PyMongo, I need to fetch the fields of another collection

I need to construct a query, using PyMongo, which gets data from two related collections in a MongoDB database. Collection X has fields UserId, Name, and EmailId: [ { "UserId" : "941AB", "Name" : "Alex Andresson", "EmailId" : …
2
votes
1 answer

parse json output for primary and secondary hosts from replSetGetStatus

I've used pymongo to connect to mongo replica set and print the status of replica set using json dump. I want to parse this output and display "name" and "stateStr" into a list or array for the user to be able to pick a particular host.Here is my…
monicak
  • 45
  • 3
2
votes
2 answers

Mongo query in python if I use variable as value

Am trying to find documents from the mongo collection using the following query. db.collection_name.find({"id" : Id}) where Id is the variable am getting as input. But it doesn't work. If I hard code the value like this db.collection_name.find({"id"…
1
vote
1 answer

MongoDB | Update rows record by record on basis of one field

I want to update the documents/records of a collection in mongodb in python with the min/max/avg of temperature on the basis of a time range. In the below example suppose time range is given to me "20:09-20:15", then the last row will not be updated…
1
vote
1 answer

using $nearSphere + limit in a mongodb find query

I've created a query with $nearSphere and I realized that I get sometimes enormous amount of results (1000+), which affects my app's performance. I've tried to use .limit() but it seems to be ignored by the query. I also tried to use batch_size()…
Amit be
  • 419
  • 2
  • 13
1
vote
0 answers

mongodb $addToSet failure, specify full document to insert

I've done a bit of research on this and haven't come across anything that jumps out at me immediately as what I'm looking for. Say we have a document (or documents) in a collection that look something like this: //First example document { "_id":…
claymonstr
  • 11
  • 2
1
vote
1 answer

How to find and count in MongoDB for specific items?

I would like to count the total number of each type of responses associated with each ID in the following JSON result that I am extracting from MongoDB: { "test": [ { "ID": 4, "response": "A" }, { "ID": 4, …
kaoscify
  • 1,615
  • 6
  • 32
  • 64
0
votes
1 answer

PyMongo: Pull an object in an array of arrays

Let's say I have a collection that looks like this (essentially a double-nested array holding objects): { 'customer': 'bob', 'products': [ { 'id': 5, 'variants': [ {'name': 'blue', 'id': 23}, {'name':…
0
votes
1 answer

Get latest Document with respect to (time or Id) using Pymongo only

In MongoDB, this technique is commonly used to obtain the latest document with respect to (time or ID): db.collection.find().sort({ "_id": -1 }).limit(1); MySchema.find().sort({ _id: -1…
harry
  • 31
  • 4
0
votes
0 answers

Test cases are failing using mongomock pymongo mongoengine

I am trying to run test cases with mongomock having pymongo version as 2.X and mongoengine 0.10.6 I have tried to change the version for pymongo to 2.9.5 from 2.6.3 and mongoengine from 0.10.6 to 0.17.0. I have tried with mongomock from 3.0.0 to…
0
votes
1 answer

pymongo where clause with a complex function

How do I write the below search query using pymongo? This query works well for me in the db. {$where: function() { var deepIterate = function (obj, value) { for (var field in obj) { if (obj[field] == value){ return true; …
Veena
  • 105
  • 9
0
votes
1 answer

update commands works via mongo shell but not via pymongo

I'm trying to update an array inside a mongo document by using pymongo but it is not working, but copy same query to robomongo does works. (it returns {'n': 1, 'nModified': 0, 'ok': 1.0, 'updatedExisting':…
Shai M.
  • 1,164
  • 3
  • 13
  • 28
0
votes
1 answer

Efficiently convert field value to integer from mongo find() using python

Given mongo document like below, how to efficiently find all documents and return student_id field as integer? { "_id" : ObjectId("58dd757910d81946b8ff853a"), "student_id": "4169506398", "first_name": "steven", "last_name":…
Sheshank Kodam
  • 445
  • 4
  • 14
0
votes
1 answer

pymongo 2.8 - how to avoid inserting duplicate user in admin db

I'm trying to write a Python script to add a user to MongoDB using pymongo 2.8. Here is my code: db = conn['test123'] collection=db['testing'] db1 = conn['admin'] try: data = db1.add_user('test123user', 'test123pass',…
monicak
  • 45
  • 3
0
votes
1 answer

Pymongo Return documents count

I want to return the total number of documents in a mongodb collection for the last 30 days and last one year based on ValidationDate . Here is my sample data { "_id": { "$oid": "570480d5d487dd2c577a4d5f" }, "ValidationDate": { …
user1895915
  • 89
  • 11
1
2