pymongo

Pymongo get inserted id's even with duplicate key error

▼魔方 西西 提交于 2021-02-11 15:15:51
问题 I am working on a flask app and using mongodb with it. In one endpoint i took csv files and inserts the content to mongodb with insert_many() . Before inserting i am creating a unique index for preventing duplication on mongodb. When there is no duplication i can reach inserted_ids for that process but when it raises duplication error i get None and i can't get inserted_ids . I am using ordered=False also. Is there any way that allows me to get inserted_ids even with duplicate key error ? def

How to change elements of an array field to values of a dict with a single attribute in MongoDB

寵の児 提交于 2021-02-11 09:12:23
问题 I need a way to change the elements of an array field to values of a dict with a single attribute. I don't have to write the result back into my table. I just have to read it that way. My table has rows like this : {a: 1, b:[ {...}, ..., {...} ], c: 2} I need a query that returns each row rewritten like this : {a: 1, b: [ {foo: { ... }}, ..., {foo: {...}} ], c: 2} In other words, each element of b becomes a dict with a single attribute, foo . This feels like a job for $project or $replaceRoot

How to change elements of an array field to values of a dict with a single attribute in MongoDB

荒凉一梦 提交于 2021-02-11 09:11:09
问题 I need a way to change the elements of an array field to values of a dict with a single attribute. I don't have to write the result back into my table. I just have to read it that way. My table has rows like this : {a: 1, b:[ {...}, ..., {...} ], c: 2} I need a query that returns each row rewritten like this : {a: 1, b: [ {foo: { ... }}, ..., {foo: {...}} ], c: 2} In other words, each element of b becomes a dict with a single attribute, foo . This feels like a job for $project or $replaceRoot

How to change elements of an array field to values of a dict with a single attribute in MongoDB

落爺英雄遲暮 提交于 2021-02-11 09:10:26
问题 I need a way to change the elements of an array field to values of a dict with a single attribute. I don't have to write the result back into my table. I just have to read it that way. My table has rows like this : {a: 1, b:[ {...}, ..., {...} ], c: 2} I need a query that returns each row rewritten like this : {a: 1, b: [ {foo: { ... }}, ..., {foo: {...}} ], c: 2} In other words, each element of b becomes a dict with a single attribute, foo . This feels like a job for $project or $replaceRoot

Error getting MongoDB by _Id in Flask

北城余情 提交于 2021-02-10 20:22:43
问题 I can query my MongoDB and I see the "_id" value like this: "_id" : BinData(3,"sFgVQWMKzUiWl5dql62j2g==") Using Flask 0.10.1 and PyMongo 3.0.3 I attempt to "find_one" like this: record = db.collection.find_one({'_id': ObjectId("sFgVQWMKzUiWl5dql62j2g==")}) And I get this error: bson.errors.InvalidId: 'sFgVQWMKzUiWl5dql62j2g==' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string Any help would be appreciated. 回答1: You are storing _id as bindata and trying to

Django + Mongo + Docker getting pymongo.errors.ServerSelectionTimeoutError

眉间皱痕 提交于 2021-02-10 16:17:16
问题 I've been struggling to get a simple app running using Django, Djongo, Mongo, and Docker Compose. My setup looks like this: docker-compose.yml services: mongodb: image: mongo:latest restart: always environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: mongoadmin MONGO_INITDB_DATABASE: django_mongodb_docker ports: - 27017:27017 web: build: ./src restart: always command: python src/manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - 8000:8000 links: - mongodb

Insert data by pymongo using mongoengine ORM in pyramid

邮差的信 提交于 2021-02-10 15:54:50
问题 I want to use pymongo connection and methods to work with mongodb, but at the same time i want using mongoengine ORM. Sample: class User(Document): email = StringField(required=True) first_name = StringField(max_length=50) last_name = StringField(max_length=50) john = User(email='jonhd@example.com') john.first_name = 'Jonh' john.last_name = 'Hope' And now i want to insert new formed document User into my 'test_collection'. In case using only mongoengine i can do this: connect('test_database')

Where to store MongoClient in Django

有些话、适合烂在心里 提交于 2021-02-10 07:37:07
问题 I'm using pymongo to allow my Django site to save data to MongoDB. Apparently the MongoClient() class has connection pooling built in, and it should only be instantiated once when Django starts up. So each connection to my Django site will basically reuse that single MongoClient. I see lots of information on the web that states that this is the way it should be done.However, I cannot find any suggestions on where exactly in Django to put this single instance of MongoClient. Most Django

MongoDB MapReduce - Emit one key/one value doesnt call reduce

﹥>﹥吖頭↗ 提交于 2021-02-05 18:54:49
问题 So i'm new with mongodb and mapreduce in general and came across this "quirk" (or atleast in my mind a quirk) Say I have objects in my collection like so: {'key':5, 'value':5} {'key':5, 'value':4} {'key':5, 'value':1} {'key':4, 'value':6} {'key':4, 'value':4} {'key':3, 'value':0} My map function simply emits the key and the value My reduce function simply adds the values AND before returning them adds 1 (I did this to check to see if the reduce function is even called) My results follow: {'

MongoDB MapReduce - Emit one key/one value doesnt call reduce

我的梦境 提交于 2021-02-05 18:54:11
问题 So i'm new with mongodb and mapreduce in general and came across this "quirk" (or atleast in my mind a quirk) Say I have objects in my collection like so: {'key':5, 'value':5} {'key':5, 'value':4} {'key':5, 'value':1} {'key':4, 'value':6} {'key':4, 'value':4} {'key':3, 'value':0} My map function simply emits the key and the value My reduce function simply adds the values AND before returning them adds 1 (I did this to check to see if the reduce function is even called) My results follow: {'