Python과 MongoDB 연동 관련 레퍼런스

2019. 3. 29. 16:46서버 프로그래밍

Javascript 프로그래밍을 할때 MongoDB를 많이 연동을 했기에, Python으로 MongoDB를 연동하는 부분에 있어서 불편한 점이 있는 것은 사실이다.


파이썬으로 mongoDB 제어하기 - pymongo 라이브러리

https://www.fun-coding.org/mongodb_basic5.html

https://www.joinc.co.kr/w/Site/Python/pymongo

http://api.mongodb.com/python/current/tutorial.html


* "pymongo auth failed in python script"라는 에러 발생하며 쿼리 실행이 안되는 문제

https://stackoverflow.com/questions/40346767/pymongo-auth-failed-in-python-script

client = MongoClient("mongodb://user_name:user_password@SERVER_IP/prod-db")
db = client['prod-db']

서버 URL에서 DB 이름을 빼도 접속이 되는 것 같아서 뺀 것이 문제였다. Node.js에서처럼 동일하게 사용하면 해결된다.


pymongo sort and find_one issue

https://stackoverflow.com/questions/10079107/pymongo-sort-and-find-one-issue

filterdict = {'email' : 'this.is@me.com'}
collection.find_one(filterdict, sort=[('lastseen', 1)])


* find 명령 결과가 Dict가 아닌 Cursor로 넘어오는 문제 : JSON 문자열로 바꾸었다가 다시 JSON 객체로 바꿔서 사용해야 한다. (도대체 왜?)

import json
from bson import json_util

docs_list  = list(db.units.find())
return json.dumps(docs_list, default=json_util.default)

https://stackoverflow.com/questions/11280382/object-is-not-json-serializable?rq=1

import json

d = json.loads(j)
print d['glossary']['title']

https://stackoverflow.com/questions/4528099/convert-string-to-json-using-python

from bson.json_util import dumps, loads

for mongo_doc in await cursor.to_list(length=10):
    # mongo_doc is a <class 'dict'> returned from the async mongo driver, in this acse motor / pymongo.
    # result of executing a simple find() query.

    json_string = dumps(mongo_doc)
    # serialize the <class 'dict'> into a <class 'str'> 

    back_to_dict = loads(json_string)
    # to unserialize, thus return the string back to a <class 'dict'> with the original 'ObjectID' type.

https://stackoverflow.com/questions/4404742/how-do-i-turn-mongodb-query-into-a-json


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

Easy

import pymongo
conn = pymongo.MongoClient()
db = conn.test #test is my database
col = db.spam #Here spam is my collection
array = list(col.find())

print array


몽고디비 쿼리 관련

$elemMatch 연산자 관련 : value로 배열 데이터를 가지고 있을 때 필요한 연산자

https://velopert.com/479

db.articles.find(
    {
        "comments": {
            $elemMatch: { "name": "Charlie" }
        }
    },
    {
        "title": true,
        "comments.name": true,
        "comments.message": true
    }
)

배열 수정 연산자 : $

https://www.zerocho.com/category/MongoDB/post/57a46d287c4a5115004e97eb

db.zero.update({ list: 2 }, { 'list.$': 5 }) // list: [1, 5, 3]


기타

How to remove a key from a Python dictionary?

https://stackoverflow.com/questions/11277432/how-to-remove-a-key-from-a-python-dictionary

If you need to remove a lot of keys from a dictionary in one line of code, I think using map() is quite succinct and Pythonic readable:

myDict = {'a':1,'b':2,'c':3,'d':4}
map(myDict.pop, ['a','c']) # The list of keys to remove
>>> myDict
{'b': 2, 'd': 4}

And if you need to catch errors where you pop a value that isn't in the dictionary, use lambda inside map() like this:

map(lambda x: myDict.pop(x,None), ['a','c','e'])
[1, 3, None] # pop returns
>>> myDict
{'b': 2, 'd': 4}


search by ObjectId in mongodb with pymongo

https://stackoverflow.com/questions/16073865/search-by-objectid-in-mongodb-with-pymongo

I use pymongo 2.4.1.

from bson.objectid import ObjectId
[i for i in dbm.neo_nodes.find({"_id": ObjectId(obj_id_to_find)})]


배열 안의 object의 field를 업데이트 하는 방법

https://blog.kjslab.com/134

1. 배열은 구분자를 통해서 가능하다.

    db.collection.update(
        { "_id" : ObjectId("5308595e3256e758757b4d2f") }, 
        { 
            "$set": { 
                "Employees.0.name " : "abc",
                "Employees.1.name " : "abc",
                "Employees.2.name " : "abc"
            }
        } 
    );

2. $ 를 사용하면 배열 전체에 update가 가능하다.

     db.collection.update(
        { 
            "_id" : ObjectId("5308595e3256e758757b4d2f"),
            "Employees.name": { "$ne": "abc" }
        }, 
        { 
            "$set": { 
                "Employees.$.name " : "abc"
            }
        } 
    );


cursor – Tools for iterating over MongoDB query results

http://api.mongodb.com/python/current/api/pymongo/cursor.html

c[index]

See __getitem__().

__getitem__(index)

Get a single document or a slice of documents from this cursor.

Raises InvalidOperation if this cursor has already been used.

To get a single document use an integral index, e.g.:

>>> db.test.find()[50]

An IndexError will be raised if the index is negative or greater than the amount of documents in this cursor. Any limit previously applied to this cursor will be ignored.

To get a slice of documents use a slice index, e.g.:

>>> db.test.find()[20:25]

This will return this cursor with a limit of 5 and skip of 20 applied. Using a slice index will override any prior limits or skips applied to this cursor (including those applied through previous calls to this method). Raises IndexError when the slice has a step, a negative start value, or a stop value less than or equal to the start value.

Parameters:
  • index: An integer or slice index to be applied to this cursor


Pymongo find nested key (in list)

https://www.reddit.com/r/learnpython/comments/3sb5rb/pymongo_find_nested_key_in_list/