pymongo

How to allow all hosts to a replica set in mongodb

淺唱寂寞╮ 提交于 2020-06-28 03:57:31
问题 When I am connecting my replica set from local machine like this "mongodb://my_server_ip:27018/?readPreference=secondary&replicaSet=rs0&appname=MongoDB%20Compass&ssl=false" It gives me an error "Could not reach any servers" and on mongo compass "connect ECONNREFUSED 127.0.0.1:27018" I think this error comes from replica set configs, where host key is "localhost:27018" Below is the replica set member info: { "_id": 1, "host": "localhost:27018", "arbiterOnly": false, "buildIndexes": true,

Aggregate query in mongo works, does not in Pymongo

孤者浪人 提交于 2020-06-25 00:58:09
问题 I encountered a problem. I try to query this document to obtain the sum the amount and group by the LOC identifier that is outside the "COL" array. { "_id" : ObjectId("57506d74c469888f0d631be6"), "LOC" : "User001", "COL" : [ { "date" : "25/03/2016", "number" : "Folio009", "amount" : 100 }, { "date" : "25/04/2016", "number" : "Folio010", "amount" : 100 } ] } This command works in mongo but I cannot make it work in Python with the Pymongo package: Mongo query (working) db.perfiles.aggregate({"

Connection reset by Peer pymongo

浪子不回头ぞ 提交于 2020-06-24 12:15:47
问题 I have some documents which I have to fetch from mongodb and set it to memcache. Here is the code import memcache from pymongo import MongoClient db = mongo_client.job_db.JobParsedData jobs = db.find().sort("JobId", 1) def set_to_memcache_raw(jobs): print("Setting raw message to memcache") count = 0 for item in jobs: job_id = item.get('JobId') job_details = item.get('JobDetails') if job_id.strip(): count += 1 memcache_obj.set(job_id, job_details, time=72000) if count % 1000 == 0: print(

Connection reset by Peer pymongo

不打扰是莪最后的温柔 提交于 2020-06-24 12:15:29
问题 I have some documents which I have to fetch from mongodb and set it to memcache. Here is the code import memcache from pymongo import MongoClient db = mongo_client.job_db.JobParsedData jobs = db.find().sort("JobId", 1) def set_to_memcache_raw(jobs): print("Setting raw message to memcache") count = 0 for item in jobs: job_id = item.get('JobId') job_details = item.get('JobDetails') if job_id.strip(): count += 1 memcache_obj.set(job_id, job_details, time=72000) if count % 1000 == 0: print(

Pymongo: iterate over all documents in the collection

走远了吗. 提交于 2020-06-24 11:52:26
问题 I am using PyMongo and trying to iterate over (10 millions) documents in my MongoDB collection and just extract a couple of keys: "name" and "address", then output them to .csv file. I cannot figure out the right syntax to do it with find().forEach() I was trying workarounds like cursor = db.myCollection.find({"name": {$regex: REGEX}}) where REGEX would match everything - and it resulted in "Killed". I also tried cursor = db.myCollection.find({"name": {"$exist": True}}) but that did not work

How to set unique constraint for field in document nested in array?

孤人 提交于 2020-06-23 08:17:18
问题 I have a collection of documents in MongoDB that looks like: {"_id": 1, "array": [{"id": 1, "content": "..."}, {"id": 2, "content": "..."}]} {"_id": 2, "array": [{"id": 1, "content": "..."}, {"id": 2, "content": "..."}, {"a_id": 3, "content": "..."}]} and I want to ensure that there is no duplicate array.id within each document. So the provided example is ok, but the followign is not: {"_id": 1, "array": [{"id": 1, "content": "..."}, {"id": 1, "content": "..."}]} My question is how to do this

Python - Pymongo MongoDB 3.4 - NumberDecimal

好久不见. 提交于 2020-06-18 09:07:13
问题 i am using mongodb 3.4 in order to insert proper prices to my database.. According to: https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst#reading-from-extended-json i used this in my python code: 'credits': {'$numberDecimal': Decimal128('9.99')}, but throws: bson.errors.InvalidDocument: key '$numberDecimal' must not start with '$' So what is the correct syntax for inserting numberdecimal in mongodb using python ? thanks and greetings 回答1: Simply this:

Django fails to connect to remote MongoDB server using djongo

时光总嘲笑我的痴心妄想 提交于 2020-06-17 15:54:17
问题 I try to connect to use a MongoDB database for the Django project. So I follow a tutorial for changing the DATABASE from settings.py # Original DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'testDB', } Changed to # From tutorials DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'testDB', 'USERNAME': 'username', 'PASSWORD': 'password', 'HOST': 'myhostname.example', 'PORT': '27017', } } Trying to run python manage.py makemigrations python manage.py migrate All works, but no data

Group by date to get count of hits for a day, for last 7 days and for a months

别说谁变了你拦得住时间么 提交于 2020-06-17 14:09:51
问题 [{'_id': '5ebe39e41e1729d90de', 'modelId': '5ebe3536c289711579', 'lastAt': datetime.datetime(2020, 5, 15, 6, 42, 44, 79000), 'proId': '5ebe3536c2897115793dccfb', 'genId': '5ebe355ac2897115793dcd04'}, {'_id': '5ebe3a0d94fcb800fa474310', 'modelId': '5ebe3536c289711579', 'proId': '5ebe3536c2897115793d', 'genId': '5ebe355ac2897115793', 'lastAt': datetime.datetime(2020, 5, 15, 6, 43, 25, 105000)}] I want to calculate count of hits for a day, for last 7 days and for a months. As the model is same,

Group by date to get count of hits for a day, for last 7 days and for a months

北战南征 提交于 2020-06-17 14:03:40
问题 [{'_id': '5ebe39e41e1729d90de', 'modelId': '5ebe3536c289711579', 'lastAt': datetime.datetime(2020, 5, 15, 6, 42, 44, 79000), 'proId': '5ebe3536c2897115793dccfb', 'genId': '5ebe355ac2897115793dcd04'}, {'_id': '5ebe3a0d94fcb800fa474310', 'modelId': '5ebe3536c289711579', 'proId': '5ebe3536c2897115793d', 'genId': '5ebe355ac2897115793', 'lastAt': datetime.datetime(2020, 5, 15, 6, 43, 25, 105000)}] I want to calculate count of hits for a day, for last 7 days and for a months. As the model is same,