본문 바로가기
DB

[MongoDB] 기본 CRUD 방법과 예시

by 오늘의개발부 2022. 2. 4.
반응형

데이터베이스 전환

> use test;

현재 데이터베이스에 만들어진 db의 목록

> show dbs;

컬렉션 조회

> show collections;

mongodb에서 간단한 자바스크립트 명령어 실행

> var a = {age: 25};
> var n = {name: 'Ed', lang: ['c','ruby','js']};
> for(i=0; i<3; i++){ print(n.lang[i]);};

Save

MongoDB 4.2부터 db.collection.save()메서드가 deprecate 됨

> db.scores.save({a:99});
< Collection.save() is deprecated.

insertOne, replaceOne

> db.scores.insertOne({a:99});
< { acknowledged: true,
< insertedId: ObjectId("61fc8b4680b436f0831d314a") }

insertMany

> db.scores.insertMany([{name: 'jj'}, {name: 'kk'}]);

Save 명령 여러번

> for(i=0; i<10; i++){ db.scores.insertOne({a:i, exam:5})};
> db.scores.find();
< { "_id" : ObjectId("61fc8b4680b436f0831d314a"), "a" : 99 }
< { "_id" : ObjectId("61fc8db88e8d08040649e667"), "a" : 0, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e668"), "a" : 1, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e669"), "a" : 2, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e66a"), "a" : 3, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e66b"), "a" : 4, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e66c"), "a" : 5, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e66d"), "a" : 6, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e66e"), "a" : 7, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e66f"), "a" : 8, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e670"), "a" : 9, "exam" : 5 }

Find

> db.scores.find();
< { _id: ObjectId("61fc8b4680b436f0831d314a"), a: 99 }

Find 조건

비교 연산자
$gt: > $lt: < $gte: >= $lte: <= $ne: !=
포함: in, nin
or: {$or: [{a : {$lt: 3}}, ...]}
필드 존재: $exists: true

> db.scores.find({a: 99});
< { "_id" : ObjectId("61fc8b4680b436f0831d314a"), "a" : 99 }
>
> db.scores.find({a: {'$gt': 8}});
< { "_id" : ObjectId("61fc8b4680b436f0831d314a"), "a" : 99 }
< { "_id" : ObjectId("61fc8db88e8d08040649e670"), "a" : 9, "exam" : 5 }
>
> db.scores.find({a: {'$gt': 2, '$lte': 4}});
< { "_id" : ObjectId("61fc8db88e8d08040649e66a"), "a" : 3, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e66b"), "a" : 4, "exam" : 5 }
>
> db.scores.find({a : {'$in': [2, 3, 4]}});
< { "_id" : ObjectId("61fc8db88e8d08040649e669"), "a" : 2, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e66a"), "a" : 3, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e66b"), "a" : 4, "exam" : 5 }
>
> db.scores.find({$or: [{a: {$lt: 1}}, {a: {$gt: 8}}]});
< { "_id" : ObjectId("61fc8b4680b436f0831d314a"), "a" : 99 }
< { "_id" : ObjectId("61fc8db88e8d08040649e667"), "a" : 0, "exam" : 5 }
< { "_id" : ObjectId("61fc8db88e8d08040649e670"), "a" : 9, "exam" : 5 }
>
> db.scores.find({name: {$exists: true}});
< { "_id" : ObjectId("61fc8e248e8d08040649e671"), "name" : "jj" }
< { "_id" : ObjectId("61fc8e248e8d08040649e672"), "name" : "kk" }

Find 반환필드 선택

1/true: 반환 2/false: 미반환
조건 없을시 모두 반환: db.scores.find({}, {});
false 조건 시 나머지 모든 필드 반환: db.scores.find({}, {a: 0});
true 조건 시 해당 필드와 _id만 반환: db.scores.find({}, {a: 1});
true/false 혼용 불가: db.scores.find({}, {a: 0, name: 1}); ❌
_id 필드만 혼용 가능: db.scores.find({}, {a: 1, _id: 0}); ✅

> db.scores.find({}, {a: 1});

Update

update: Document 전체 내용 변경
db.users.update({조건}, {치환할 데이터})

> db.users.save({name: 'John', lang: ['ruby','c']});
> db.users.save({name: 'Jack', lang: ['java','js']});
>
> db.users.update({name: 'John'}, {name: 'Cash', lang: ['eng']});
< { "_id" : ObjectId("61fcb41b8e8d08040649e674"), "name" : "Cash", "lang" : [ "eng" ] }
< { "_id" : ObjectId("61fcb4208e8d08040649e675"), "name" : "Jack", "lang" : [ "java", "js" ] }

특정 프로퍼티만 변경: db.users.update({조건}, {$set: {업데이트할 데이터}})
특정 프로퍼티 삭제: db.users.update({조건}, {$unset: {업데이트할 데이터}})
배열에 든 값 삭제: db.users.update({조건}, {$pull: {'배열 이름': '삭제할값'}})
배열에 든 값 추가: db.users.update({조건}, {$push: {'배열 이름': '추가할값'}})

> db.users.update({name: 'Cash'}, {$set: {age: 50}});
< { "_id" : ObjectId("61fcb41b8e8d08040649e674"), "name" : "Cash", "lang" : [ "eng" ], "age" : 50 }
< { "_id" : ObjectId("61fcb4208e8d08040649e675"), "name" : "Jack", "lang" : [ "java", "js" ] }
>
> db.users.update({name: 'Cash'}, {$unset: {age: 50}});
< { "_id" : ObjectId("61fcb41b8e8d08040649e674"), "name" : "Cash", "lang" : [ "eng" ] }
< { "_id" : ObjectId("61fcb4208e8d08040649e675"), "name" : "Jack", "lang" : [ "java", "js" ] }
>
> db.users.update({name: 'Jack'}, {$pull: {'lang': 'java'}});
< { "_id" : ObjectId("61fcb41b8e8d08040649e674"), "name" : "Cash", "lang" : [ "eng" ] }
< { "_id" : ObjectId("61fcb4208e8d08040649e675"), "name" : "Jack", "lang" : [ "js" ] }
>
> db.users.update({name: 'Jack'}, {$push: {'lang': 'java'}});
< { "_id" : ObjectId("61fcb41b8e8d08040649e674"), "name" : "Cash", "lang" : [ "eng" ] }
< { "_id" : ObjectId("61fcb4208e8d08040649e675"), "name" : "Jack", "lang" : [ "js", "java" ] }

Remove

조건부 삭제: db.users.remove({조건});
전체 삭제: db.users.remove({});

> db.users.remove({name: 'Jack'});
> db.users.remove({});
반응형