- CRUD
- save와 insert가 다른점
- save는 id가 있으면 update, 없으면 insert 된다.
- insert는 무조건 id가 생긴다.
> show dbs
local 0.078125GB
personDB 0.203125GB
persons 0.203125GB
test 0.203125GB
> use mydb;
switched to db mydb
> db
mydb
> j={name:"mongo"};
{ "name" : "mongo" }
> k={x:3}
{ "x" : 3 }
> k
{ "x" : 3 }
> j
{ "name" : "mongo" }
> db.things.insert(j);
>
> show dbs;
local 0.078125GB
mydb 0.203125GB
personDB 0.203125GB
persons 0.203125GB
test 0.203125GB
>
> db.things.insert(k);
> show collections;
system.indexes
things
> db.things.find();
{ "_id" : ObjectId("52dd4b460e922248291cc5f1"), "name" : "mongo" }
{ "_id" : ObjectId("52dd4be00e922248291cc5f2"), "x" : 3 }
>
- 컬렉션을 프로그램으로 넣을 수 있다.
> for(var i=1; i<=20; i++) db.things.insert({x:4,j:i});
> db.things.find();
{ "_id" : ObjectId("52dd4b460e922248291cc5f1"), "name" : "mongo" }
{ "_id" : ObjectId("52dd4be00e922248291cc5f2"), "x" : 3 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f3"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f4"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f5"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f6"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f7"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f8"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f9"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fa"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fb"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fc"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fd"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fe"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("52dd4c550e922248291cc5ff"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("52dd4c550e922248291cc600"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("52dd4c550e922248291cc601"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("52dd4c550e922248291cc602"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("52dd4c550e922248291cc603"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("52dd4c550e922248291cc604"), "x" : 4, "j" : 18 }
Type "it" for more
> it
{ "_id" : ObjectId("52dd4c550e922248291cc605"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("52dd4c550e922248291cc606"), "x" : 4, "j" : 20 }
>
- Cursor
> var c=db.things.find();
> c
{ "_id" : ObjectId("52dd4b460e922248291cc5f1"), "name" : "mongo" }
{ "_id" : ObjectId("52dd4be00e922248291cc5f2"), "x" : 3 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f3"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f4"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f5"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f6"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f7"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f8"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f9"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fa"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fb"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fc"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fd"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fe"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("52dd4c550e922248291cc5ff"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("52dd4c550e922248291cc600"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("52dd4c550e922248291cc601"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("52dd4c550e922248291cc602"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("52dd4c550e922248291cc603"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("52dd4c550e922248291cc604"), "x" : 4, "j" : 18 }
Type "it" for more
> it
{ "_id" : ObjectId("52dd4c550e922248291cc605"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("52dd4c550e922248291cc606"), "x" : 4, "j" : 20 }
>
- while
- 커서에 담긴 것을 출력하는 것이다. 미리 출력했으면 출력할게 없다.
> while(c.hasNext()) printjson(c.next());
> var c=db.things.find();
> while(c.hasNext()) printjson(c.next());
{ "_id" : ObjectId("52dd4b460e922248291cc5f1"), "name" : "mongo" }
{ "_id" : ObjectId("52dd4be00e922248291cc5f2"), "x" : 3 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f3"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f4"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f5"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f6"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f7"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f8"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f9"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fa"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fb"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fc"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fd"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fe"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("52dd4c550e922248291cc5ff"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("52dd4c550e922248291cc600"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("52dd4c550e922248291cc601"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("52dd4c550e922248291cc602"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("52dd4c550e922248291cc603"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("52dd4c550e922248291cc604"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("52dd4c550e922248291cc605"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("52dd4c550e922248291cc606"), "x" : 4, "j" : 20 }
>
- find()는 data가 사라지는 것과 달리 data를 직접 파일시스템에 가서 보는 것이다.
- ensure 인덱스 개념을 가져간다.
> db.things.find({name:"mongo"});
{ "_id" : ObjectId("52dd4b460e922248291cc5f1"), "name" : "mongo" }
> db.things.find({x:3});
{ "_id" : ObjectId("52dd4be00e922248291cc5f2"), "x" : 3 }
>
- find()를 하면 data를 볼 수 있다.
- 두번째 {} 에서는 key에 대해서 1은 보여주기, 0은 안 보기 를 설정한다.
> db.things.find({x:3},{});
{ "_id" : ObjectId("52dd4be00e922248291cc5f2"), "x" : 3 }
> db.things.find({x:3}, {x:1,_id:0});
{ "x" : 3 }
>
- x값이 4인 것중에 j의 값은 보여준다.
> db.things.find({x:4},{j:1});
{ "_id" : ObjectId("52dd4c550e922248291cc5f3"), "j" : 1 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f4"), "j" : 2 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f5"), "j" : 3 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f6"), "j" : 4 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f7"), "j" : 5 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f8"), "j" : 6 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f9"), "j" : 7 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fa"), "j" : 8 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fb"), "j" : 9 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fc"), "j" : 10 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fd"), "j" : 11 }
{ "_id" : ObjectId("52dd4c550e922248291cc5fe"), "j" : 12 }
{ "_id" : ObjectId("52dd4c550e922248291cc5ff"), "j" : 13 }
{ "_id" : ObjectId("52dd4c550e922248291cc600"), "j" : 14 }
{ "_id" : ObjectId("52dd4c550e922248291cc601"), "j" : 15 }
{ "_id" : ObjectId("52dd4c550e922248291cc602"), "j" : 16 }
{ "_id" : ObjectId("52dd4c550e922248291cc603"), "j" : 17 }
{ "_id" : ObjectId("52dd4c550e922248291cc604"), "j" : 18 }
{ "_id" : ObjectId("52dd4c550e922248291cc605"), "j" : 19 }
{ "_id" : ObjectId("52dd4c550e922248291cc606"), "j" : 20 }
>
- 오퍼레이션은 확장객체이다. 확장객체 개념으로 받아 들여야 한다.
- x값이 4인 것중에 j의 값은 보여주고, _id는 보여주지 않는다.
> db.things.find({x:4},{j:1,_id:0});
{ "j" : 1 }
{ "j" : 2 }
{ "j" : 3 }
{ "j" : 4 }
{ "j" : 5 }
{ "j" : 6 }
{ "j" : 7 }
{ "j" : 8 }
{ "j" : 9 }
{ "j" : 10 }
{ "j" : 11 }
{ "j" : 12 }
{ "j" : 13 }
{ "j" : 14 }
{ "j" : 15 }
{ "j" : 16 }
{ "j" : 17 }
{ "j" : 18 }
{ "j" : 19 }
{ "j" : 20 }
>
- things에서 3개를 skip하고 한개만 보여준다.
> db.things.find().skip(3).limit(1);
{ "_id" : ObjectId("52dd4c550e922248291cc5f4"), "x" : 4, "j" : 2 }
- things에서 3개를 skip하고 2개만 보여준다.
> db.things.find().skip(3).limit(2);
{ "_id" : ObjectId("52dd4c550e922248291cc5f4"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("52dd4c550e922248291cc5f5"), "x" : 4, "j" : 3 }
>
- people data를 만들어 본다.
> show collections
system.indexes
things
> db.people.insert({age:10});
> db.people.insert({age:20});
> db.people.insert({age:30});
> db.people.insert({age:40});
> db.people.insert({age:50});
> db.people.insert({age:60});
> db.people.find();
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
>
- numerical operators
- 크고 작은 것
$gt -> Greater then (>)
$gte -> Greater then or Equal(>=)
$lt -> Less then (<)
> db.people.find({age:20});
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
> db.people.find({age:{$gt:20}});
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
> db.people.find({age:{$gte:30}});
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
> db.people.find({age:{$lt:30}});
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
> db.people.find({age:{$lte:30}});
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
>
- mod
> db.people.find({age:{$mod:[2,0]}});
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
> db.people.find({age:{$mod:[2,1]}});
> db.people.find({age:{$mod:[1,0]}});
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
> db.people.find({age:{$mod:[3,0]}});
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
> db.people.find({age:{$mod:[4,0]}});
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
>
- logical operators
$and -> logical and
$or -> logical or
$not -> logical not(can only be use with other operators)
> db.people.find({$or:[{age:20,age:50}]});
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
>
> db.people.find({$not:{$gt:30}});
> db.people.find({age:{$not:{$gt:30}}});
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
>
-- conditional operators
$in -> inside or find in an array. analogous to in() In the relacional world
$nin -> not inside
$all -> find all elements in an array
$size -> matches any array with the specified number of elements
$exits -> check the existence of a field
> db.people.find({});
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
> db.test.insert({values:['a','b','c','d']});
> db.test.find();
{ "_id" : ObjectId("52dd9f662806f69444e84413"), "values" : [ "a", "b", "c",
"d" ] }
> db.test.insert({values:['b','c']});
> db.test.insert({values:['d']});
> db.test.insert({values:['a','d']});
> db.test.insert({values:['a','d'],v:'helele value'});
> db.test.find();
{ "_id" : ObjectId("52dd9f662806f69444e84413"), "values" : [ "a", "b", "c",
"d" ] }
{ "_id" : ObjectId("52dd9f8e2806f69444e84414"), "values" : [ "b", "c" ] }
{ "_id" : ObjectId("52dd9fa42806f69444e84415"), "values" : [ "d" ] }
{ "_id" : ObjectId("52dd9fb12806f69444e84416"), "values" : [ "a", "d" ] }
{ "_id" : ObjectId("52dd9fce2806f69444e84417"), "values" : [ "a", "d" ], "v" :
"helele value" }
>
> db.test.find().pretty();
{
"_id" : ObjectId("52dd9f662806f69444e84413"),
"values" : [
"a",
"b",
"c",
"d"
]
}
{
"_id" : ObjectId("52dd9f8e2806f69444e84414"),
"values" : [
"b",
"c"
]
}
{ "_id" : ObjectId("52dd9fa42806f69444e84415"), "values" : [ "d" ] }
{
"_id" : ObjectId("52dd9fb12806f69444e84416"),
"values" : [
"a",
"d"
]
}
{
"_id" : ObjectId("52dd9fce2806f69444e84417"),
"values" : [
"a",
"d"
],
"v" : "helele value"
}
>
> db.test.find({});
{ "_id" : ObjectId("52dd9f662806f69444e84413"), "values" : [ "a", "b", "c",
"d" ] }
{ "_id" : ObjectId("52dd9f8e2806f69444e84414"), "values" : [ "b", "c" ] }
{ "_id" : ObjectId("52dd9fa42806f69444e84415"), "values" : [ "d" ] }
{ "_id" : ObjectId("52dd9fb12806f69444e84416"), "values" : [ "a", "d" ] }
{ "_id" : ObjectId("52dd9fce2806f69444e84417"), "values" : [ "a", "d" ], "v" :
"helele value" }
- values에 a가 없는 경우
> db.test.find({values:{$nin:['a']}});
{ "_id" : ObjectId("52dd9f8e2806f69444e84414"), "values" : [ "b", "c" ] }
{ "_id" : ObjectId("52dd9fa42806f69444e84415"), "values" : [ "d" ] }
- values에 b,c가 모두 들어 있는 경우
> db.test.find({values:{$all:['b','c']}});
{ "_id" : ObjectId("52dd9f662806f69444e84413"), "values" : [ "a", "b", "c",
"d" ] }
{ "_id" : ObjectId("52dd9f8e2806f69444e84414"), "values" : [ "b", "c" ] }
- people에서 age가 30보다 큰 경우
> db.people.find({$where:'this.age>30'});
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
>
- 업데이트는 먼저 읽고 처리한다.
- update는 두 종류이다.
- document 전체 upsert 없으면 insert 있으면 update
자료 강의
- _id를 지정해서 insert 할 수 있다.
> db.people.insert({_id:1,names:['lee','kim','hwang','park'],qdt:3});
> db.people.insert({_id:1,names:['kim1','lee1','toitoi','son'],qdt:3});
E11000 duplicate key error index: mydb.people.$_id_ dup key: { : 1.0 }
> db.people.insert({_id:2,names:['kim1','lee1','toitoi','son'],qdt:3});
> db.people.insert({_id:3,names:['kim2','lee2','toitoi1','son1'],qdt:3});
> db.people.find();
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park" ], "qdt" : 3 }
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi", "son" ], "qdt" : 3 }
{ "_id" : 3, "names" : [ "kim2", "lee2", "toitoi1", "son1" ], "qdt" : 3 }
>
- $inc
- update
> db.people.find({_id:1});
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park" ], "qdt" : 3 }
> db.people.update({_id:1},{$inc:{qdt:1}});
> db.people.find();
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park" ], "qdt" : 4 }
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi", "son" ], "qdt" : 3 }
{ "_id" : 3, "names" : [ "kim2", "lee2", "toitoi1", "son1" ], "qdt" : 3 }
>
-$set
> db.people.update({"_id":ObjectId("52dd4feb0e922248291cc60b")}, {$set:{qdt:10}}
);
> db.people.update({"_id":ObjectId("52dd4fee0e922248291cc60c")}, {$set:{qdt:20}}
);
> db.people.find();
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park" ], "qdt" : 4 }
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi", "son" ], "qdt" : 3 }
{ "_id" : 3, "names" : [ "kim2", "lee2", "toitoi1", "son1" ], "qdt" : 3 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50, "qdt" : 10 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60, "qdt" : 20 }
>
> db.people.update({qdt:10},{$set:{qdt:210}});
> db.people.find();
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park" ], "qdt" : 4 }
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi", "son" ], "qdt" : 3 }
{ "_id" : 3, "names" : [ "kim2", "lee2", "toitoi1", "son1" ], "qdt" : 3 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50, "qdt" : 210 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60, "qdt" : 20 }
>
- 4번째인자가 멀티플 db를 업데이트 할 것인지를 결정한다.
> db.people.update({qdt:20},{$set:{qdt:210}},false,true);
> db.people.find();
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park" ], "qdt" : 4 }
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi", "son" ], "qdt" : 3 }
{ "_id" : 3, "names" : [ "kim2", "lee2", "toitoi1", "son1" ], "qdt" : 3 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50, "qdt" : 210 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60, "qdt" : 210 }
[mongodb] 강좌3 BigData MongoDB(NoSQL)
'database > mongodb' 카테고리의 다른 글
[mongodb] 강좌5 BigData MongoDB(NoSQL) (0) | 2014.03.03 |
---|---|
[mongodb] 강좌4 BigData MongoDB(NoSQL) (0) | 2014.03.03 |
[mongodb] 강좌2 BigData MongoDB(NoSQL) (0) | 2014.03.03 |
[mongodb] 강좌1 BigData MongoDB(NoSQL) 설치하기 (0) | 2014.03.03 |