'DB/MongoDB'에 해당되는 글 6건

  1. 2015.03.16 :: [mongodb] pymongo 예시
  2. 2015.03.16 :: [mongodb] command 요약
  3. 2015.03.16 :: [mongodb] 데이터 모델링
  4. 2015.03.03 :: [mongodb] mongo 결과 파일 출력
  5. 2015.01.07 :: [mongodb] 용어&쿼리비교
  6. 2015.01.07 :: mongodb 설치
DB/MongoDB 2015. 3. 16. 11:56

pymongo site : http://api.mongodb.org/python/current/



# ?/usr/bin/python

import pymongo


class Database():

    def __init__(self, address=DB_DEFAULT_ADDRESS, port=DB_DEFAULT_PORT):

        self.port = port

        self.address = address

          

    def getDb(self, dbName=DB_DEFAULT_NAME):

        client = pymongo.MongoClient(self.address, self.port)

        return client[dbName]

    

class DbQuery():

    def __init__(self, db):

        self.db = db

        

    def insert(self, col, val):

        try:

            self.db[col].insert(val)

        except Exception as e:

            return e.args[0]

        

    def remove(self, col, id):

        try:

            self.db[col].remove(where)

        except Exception as e:

            return e.args[0]

    

    def set(self, col, where, val):

        try:

            self.db[col].update(where, {"$set":val})

        except Exception as e:

            return e.args[0]

    

    def push(self, col, where, val):

        try:

            self.db[col].update(where, {"$push": val}, True)

        except Exception as e:

            return e.args[0]

        

    def pull(self, col, where, val):

        try:

            self.db[col].update(where, {"$pull": val}, True)

        except Exception as e:

            return e.args[0]

        

    def find(self, col, where, val):

        try:

            return self.db[col].find(where, val)

        except Exception as e:

            return e.args[0]

        return e.args[0]

    

    def findOne(self, col, where, val):

        try:

            return self.db[col].find_one(where, val)

        except Exception as e:

            return e.args[0]

        return e.args[0]

    

    def inc(self, col, where, val):

        try:

            self.db[col].update(where, {"$inc":val})

        except Exception as e:

            return e.args[0]


'DB > MongoDB' 카테고리의 다른 글

[mongodb] command 요약  (0) 2015.03.16
[mongodb] 데이터 모델링  (0) 2015.03.16
[mongodb] mongo 결과 파일 출력  (0) 2015.03.03
[mongodb] 용어&쿼리비교  (0) 2015.01.07
mongodb 설치  (0) 2015.01.07
posted by cozyboy
:
DB/MongoDB 2015. 3. 16. 11:51

mongodb 레퍼런스 : 


http://docs.mongodb.org/manual/reference/operator/query/

http://docs.mongodb.org/manual/reference/operator/update/






현재 DB 확인

db

 

DB 리스트 확인

show dbs

 

DB 사용 생성

use mydb

 

DB 삭제

use mydb;

db.dropDatabase();

 

 

collection(Table) 생성

db.createCollection("job")

 


collection 리스트 보기

use mydb

show collections   


collection 이름 수정

db.job.renameCollection("newJob")

 

collection 삭제

db.job.drop();

 

collection 상태 보기

db.job.validate();



document(row) 삽입

db.job.insert( {key: "라이징오", url:"a.com", rank:3,  e_date:[2.100,3.2,4.4, 0.20] })


document 삭제

db.mycol.remove( { key: "라이징오" }, 1 )       //FIFO 삭제됨


 

update   (set은 값 자체를 변경, push는 삽입, unset은 row 제거, pull은 배열내에 특정 요소 제거)

db.mycol.update( {_id:"cozy"},  {$set: {key:"라이징오", "e_date.1": 0 } } )  //"e_date.1" : 1번째 인덱스 변경

 

db.mycol.update({'_id':'cozy', 'r_rate_time.date':'2014-12-30'}, {$push:{'r_rate_time.rate':3} } )



db.members.update(
    {
"user_id" : "{1231mjnD-32JIjn-3213}", "campaigns.campaign_id": 3221},
    {$push:{
"campaigns.$.messages":{"message_id":4213122, "email":"john@gmail.com"}}}
)

push-to-array-inside-array

출처: <http://stackoverflow.com/questions/9209670/mongo-push-to-array-inside-array



해당 컬럼 삭제

db.mycol.update( {_id:"cozy"},  {$unset: {e_date:1} } )


해당 컬럼에서 배열요소 제거

db.mycol.update({_id:"cozy"}, {$pull : {votes:{ $gte: 6}}} )


inc, dec

db.products.update( { _id: "cozy" },  { $inc: { quantity: -2} })







'DB > MongoDB' 카테고리의 다른 글

[mongodb] pymongo 예시  (0) 2015.03.16
[mongodb] 데이터 모델링  (0) 2015.03.16
[mongodb] mongo 결과 파일 출력  (0) 2015.03.03
[mongodb] 용어&쿼리비교  (0) 2015.01.07
mongodb 설치  (0) 2015.01.07
posted by cozyboy
:
DB/MongoDB 2015. 3. 16. 11:26


References

References store the relationships between data by including

links or references from one document to another. 

Applications can resolve these references to access the 

related data. Broadly, these arenormalized data models.


See Normalized Data Models for the strengths and 

weaknesses of using references.


Embedded Data

Embedded documents capture relationships between data 

by storing related data in a single document structure. 

MongoDB documents make it possible to embed document

 structures as sub-documents in a field or array within a 

document. These denormalized data models allow 

applications to retrieve and manipulate related data 

in a single database operation.

See Embedded Data Models for the strengths and 

weaknesses of embedding sub-documents.

 

출처: <http://docs.mongodb.org/manual/core/data-modeling-introduction/>

 


'DB > MongoDB' 카테고리의 다른 글

[mongodb] pymongo 예시  (0) 2015.03.16
[mongodb] command 요약  (0) 2015.03.16
[mongodb] mongo 결과 파일 출력  (0) 2015.03.03
[mongodb] 용어&쿼리비교  (0) 2015.01.07
mongodb 설치  (0) 2015.01.07
posted by cozyboy
:
DB/MongoDB 2015. 3. 3. 17:13

$ mongo --quiet {dbname} --eval 'printjson(db.job.find().toArray())' > output.json


$ vi output.json



http://stackoverflow.com/questions/13104800/printing-mongodb-shell-output-to-file

'DB > MongoDB' 카테고리의 다른 글

[mongodb] pymongo 예시  (0) 2015.03.16
[mongodb] command 요약  (0) 2015.03.16
[mongodb] 데이터 모델링  (0) 2015.03.16
[mongodb] 용어&쿼리비교  (0) 2015.01.07
mongodb 설치  (0) 2015.01.07
posted by cozyboy
:
DB/MongoDB 2015. 1. 7. 13:53

1. 용어


2. 쿼리 비교





펌 : http://cafe.naver.com/hadoopkr/33

'DB > MongoDB' 카테고리의 다른 글

[mongodb] pymongo 예시  (0) 2015.03.16
[mongodb] command 요약  (0) 2015.03.16
[mongodb] 데이터 모델링  (0) 2015.03.16
[mongodb] mongo 결과 파일 출력  (0) 2015.03.03
mongodb 설치  (0) 2015.01.07
posted by cozyboy
:
DB/MongoDB 2015. 1. 7. 13:37
  1. 환경 : 우분투

 

  1. 설치

 

  • mongoDB

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

 

$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

 

$ sudo apt-get update

 

$ sudo apt-get install -y mongodb-org

 

 

  • python monogodb

 

$ sudo apt-get install python-pip

$ sudo pip install pymongo





'DB > MongoDB' 카테고리의 다른 글

[mongodb] pymongo 예시  (0) 2015.03.16
[mongodb] command 요약  (0) 2015.03.16
[mongodb] 데이터 모델링  (0) 2015.03.16
[mongodb] mongo 결과 파일 출력  (0) 2015.03.03
[mongodb] 용어&쿼리비교  (0) 2015.01.07
posted by cozyboy
: