[mongodb] 강좌4 BigData MongoDB(NoSQL)
- 컬럼을 빼고 넣어 보자
- 컬럼 빼기
> db.people.update({qdt:210}, {$unset:{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 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
>
- 보고 싶은 컬럼만 본다.
> db.people.find({}, {age:1, qdt:1, _id:0});
{ "age" : 10 }
{ "age" : 20 }
{ "age" : 30 }
{ "age" : 40 }
{ "qdt" : 4 }
{ "qdt" : 3 }
{ "qdt" : 3 }
{ "age" : 50 }
{ "age" : 60 }
>
- 0보다 큰 것만 본다.
> db.people.find({age:{$gt: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 }
- 40보다 큰 것만 본다.
> db.people.find({age:{$gt:40}});
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 50 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 60 }
>
- age가 40보다 큰 것의 age값을 1000으로 바꾸어라
- update
> db.people.update({age:{$gt:40}},{$set:{age:1000}},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" : 1000 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 1000 }
>
- _id가 1인 것을 찾고
- 그 안에 names에 'choi' 를 추가한다.
- push
> db.people.find({_id:1});
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park" ], "qdt" : 4 }
> db.people.update({_id:1}, {$push:{names:'choi'}});
> db.people.find();
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi", "son" ], "qdt" : 3 }
{ "_id" : 3, "names" : [ "kim2", "lee2", "toitoi1", "son1" ], "qdt" : 3 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 1000 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 1000 }
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park", "choi" ], "qdt" : 4
}
>
- _id가 1인 것에 다중 배열을 넣어 보자
- names가 'choi1','choi2','choi3'를 넣는다.
- $pushAll
> db.people.update({_id:1},{$pushAll:{names:['choi1','choi2','choi3']}});
> db.people.find({_id:1});
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park", "choi", "choi1",
"choi2", "choi3" ], "qdt" : 4 }
>
- 클라이언트에서 바로 웹소켓으로 직접 db에 접근하여 데이타를 가져오거나 쓸 수 있다.
- 구글안경
- 배열에서 data 하나를 빼 본다.
- $pop
> db.people.find();
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi", "son" ], "qdt" : 3 }
{ "_id" : 3, "names" : [ "kim2", "lee2", "toitoi1", "son1" ], "qdt" : 3 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 1000 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 1000 }
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park", "choi", "choi1",
"choi2", "choi3" ], "qdt" : 4 }
> db.people.find({_id:2});
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi", "son" ], "qdt" : 3 }
> db.people.update({_id:2},{$pop:{names:1}},false,true);
> db.people.find({_id:2});
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi" ], "qdt" : 3 }
>
- 테스트를 위해 data를 insert 한다.
> db.people.insert({data:[1,1,2,3,4,4,1,5,6,7,8,8]});
> db.people.find();
{ "_id" : ObjectId("52dd4fdc0e922248291cc607"), "age" : 10 }
{ "_id" : ObjectId("52dd4fdf0e922248291cc608"), "age" : 20 }
{ "_id" : ObjectId("52dd4fe20e922248291cc609"), "age" : 30 }
{ "_id" : ObjectId("52dd4fe40e922248291cc60a"), "age" : 40 }
{ "_id" : 2, "names" : [ "kim1", "lee1", "toitoi" ], "qdt" : 3 }
{ "_id" : 3, "names" : [ "kim2", "lee2", "toitoi1", "son1" ], "qdt" : 3 }
{ "_id" : ObjectId("52dd4feb0e922248291cc60b"), "age" : 1000 }
{ "_id" : ObjectId("52dd4fee0e922248291cc60c"), "age" : 1000 }
{ "_id" : 1, "names" : [ "lee", "kim", "hwang", "park", "choi", "choi1",
"choi2", "choi3" ], "qdt" : 4 }
{ "_id" : ObjectId("52df0dfced85754d5c6d0364"), "data" : [ 1, 1, 2, 3, 4,
4, 1, 5, 6, 7, 8, 8 ] }
>
- data가 1인 것을 전부 삭제하라
- $pull
> db.people.find({_id: ObjectId("52df0dfced85754d5c6d0364")});
{ "_id" : ObjectId("52df0dfced85754d5c6d0364"), "data" : [ 1, 1, 2, 3, 4,
4, 1, 5, 6, 7, 8, 8 ] }
> db.people.update({_id: ObjectId("52df0dfced85754d5c6d0364")},{$pull:{data:1}})
;
> db.people.find({_id: ObjectId("52df0dfced85754d5c6d0364")});
{ "_id" : ObjectId("52df0dfced85754d5c6d0364"), "data" : [ 2, 3, 4, 4, 5,
6, 7, 8, 8 ] }
>
- 참고 사이트
윤형식 http://mobicon.tistory.com/category/MongoDB
이복영
- Core MongoDB Operations (CRUD)
http://docs.mongodb.org/manual/crud/#crud-operations
- 이 구조는 어소씨에션 객체다.
> use tests
switched to db tests
> show collections;
> db.bios.insert(
... {
... _id: 1,
... name: { first: 'John', last: 'Backus' },
... birth: new Date('Dec 03, 1924'),
... death: new Date('Mar 17, 2007'),
... contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ],
... awards: [
... {
... award: 'W.W. McDowell Award',
... year: 1967,
... by: 'IEEE Computer Society'
... },
... {
... award: 'National Medal of Science',
... year: 1975,
... by: 'National Science Foundation'
... },
... {
... award: 'Turing Award',
... year: 1977,
... by: 'ACM'
... },
... {
... award: 'Draper Prize',
... year: 1993,
... by: 'National Academy of Engineering'
... }
... ]
... }
... )
> show collections;
bios
system.indexes
>
- bios에 data를 하나를 더 넣어준다.
> db.bios.insert(
... {
... name: { first: 'John', last: 'McCarthy' },
... birth: new Date('Sep 04, 1927'),
... death: new Date('Dec 24, 2011'),
... contribs: [ 'Lisp', 'Artificial Intelligence', 'ALGOL' ],
... awards: [
... {
... award: 'Turing Award',
... year: 1971,
... by: 'ACM'
... },
... {
... award: 'Kyoto Prize',
... year: 1988,
... by: 'Inamori Foundation'
... },
... {
... award: 'National Medal of Science',
... year: 1990,
... by: 'National Science Foundation'
... }
... ]
... }
... )
> db.bios.find().pretty();
{
"_id" : 1,
"name" : {
"first" : "John",
"last" : "Backus"
},
"birth" : ISODate("1924-12-02T15:00:00Z"),
"death" : ISODate("2007-03-16T15:00:00Z"),
"contribs" : [
"Fortran",
"ALGOL",
"Backus-Naur Form",
"FP"
],
"awards" : [
{
"award" : "W.W. McDowell Award",
"year" : 1967,
"by" : "IEEE Computer Society"
},
{
"award" : "National Medal of Science",
"year" : 1975,
"by" : "National Science Foundation"
},
{
"award" : "Turing Award",
"year" : 1977,
"by" : "ACM"
},
{
"award" : "Draper Prize",
"year" : 1993,
"by" : "National Academy of Engineering"
}
]
}
{
"_id" : ObjectId("52e060dee4d16089c04c02e9"),
"name" : {
"first" : "John",
"last" : "McCarthy"
},
"birth" : ISODate("1927-09-03T15:00:00Z"),
"death" : ISODate("2011-12-23T15:00:00Z"),
"contribs" : [
"Lisp",
"Artificial Intelligence",
"ALGOL"
],
"awards" : [
{
"award" : "Turing Award",
"year" : 1971,
"by" : "ACM"
},
{
"award" : "Kyoto Prize",
"year" : 1988,
"by" : "Inamori Foundation"
},
{
"award" : "National Medal of Science",
"year" : 1990,
"by" : "National Science Foundation"
}
]
}
>
- data가 두개 이상이면 cousor가 만들어진다.
> db.bios.find({name:{first:'John',last:'McCarthy'}});
{ "_id" : ObjectId("52e060dee4d16089c04c02e9"), "name" : { "first" : "John", "la
st" : "McCarthy" }, "birth" : ISODate("1927-09-03T15:00:00Z"), "death" : ISODate
("2011-12-23T15:00:00Z"), "contribs" : [ "Lisp", "Artificial Intelligence", "
ALGOL" ], "awards" : [ { "award" : "Turing Award", "year" : 1971,
"by" : "ACM" }, { "award" : "Kyoto Prize", "year" : 1988,
"by" : "Inamori Foundation" }, { "award" : "National Medal of Science",
"year" : 1990, "by" : "National Science Foundation" } ] }
> db.bios.find({name:{first:'John',last:'McCarthy'}}).explain();
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 2,
"nscanned" : 2,
"nscannedObjectsAllPlans" : 2,
"nscannedAllPlans" : 2,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
},
"server" : "actsoft-PC:27017"
}
>
> db.bios.ensureIndex({name:{first:'John',last:'McCarthy'}});
{
"err" : "bad index key pattern { name: { first: \"John\", last: \"McCart
hy\" } }",
"code" : 10098,
"n" : 0,
"connectionId" : 2,
"ok" : 1
}
>
- 다중 data를 insert 할 때는 다음과 같이 한다.
> db.bios.insert(
... [
... {
... _id: 3,
... name: { first: 'Grace', last: 'Hopper' },
... title: 'Rear Admiral',
... birth: new Date('Dec 09, 1906'),
... death: new Date('Jan 01, 1992'),
... contribs: [ 'UNIVAC', 'compiler', 'FLOW-MATIC', 'COBOL' ],
... awards: [
... {
... award: 'Computer Sciences Man of the Year',
... year: 1969,
... by: 'Data Processing Management Association'
... },
... {
... award: 'Distinguished Fellow',
... year: 1973,
... by: ' British Computer Society'
... },
... {
... award: 'W. W. McDowell Award',
... year: 1976,
... by: 'IEEE Computer Society'
... },
... {
... award: 'National Medal of Technology',
... year: 1991,
... by: 'United States'
... }
... ]
... },
... {
... _id: 4,
... name: { first: 'Kristen', last: 'Nygaard' },
... birth: new Date('Aug 27, 1926'),
... death: new Date('Aug 10, 2002'),
... contribs: [ 'OOP', 'Simula' ],
... awards: [
... {
... award: 'Rosing Prize',
... year: 1999,
... by: 'Norwegian Data Association'
... },
... {
... award: 'Turing Award',
... year: 2001,
... by: 'ACM'
... },
... {
... award: 'IEEE John von Neumann Medal',
... year: 2001,
... by: 'IEEE'
... }
... ]
... },
... {
... _id: 5,
... name: { first: 'Ole-Johan', last: 'Dahl' },
... birth: new Date('Oct 12, 1931'),
... death: new Date('Jun 29, 2002'),
... contribs: [ 'OOP', 'Simula' ],
... awards: [
... {
... award: 'Rosing Prize',
... year: 1999,
... by: 'Norwegian Data Association'
... },
... {
... award: 'Turing Award',
... year: 2001,
... by: 'ACM'
... },
... {
... award: 'IEEE John von Neumann Medal',
... year: 2001,
... by: 'IEEE'
... }
... ]
... }
... ]
... )
>
> db.bios.find().toArray();
[
{
"_id" : 1,
"name" : {
"first" : "John",
"last" : "Backus"
},
"birth" : ISODate("1924-12-02T15:00:00Z"),
"death" : ISODate("2007-03-16T15:00:00Z"),
"contribs" : [
"Fortran",
"ALGOL",
"Backus-Naur Form",
"FP"
],
"awards" : [
{
"award" : "W.W. McDowell Award",
"year" : 1967,
"by" : "IEEE Computer Society"
},
{
"award" : "National Medal of Science",
"year" : 1975,
"by" : "National Science Foundation"
},
{
"award" : "Turing Award",
"year" : 1977,
"by" : "ACM"
},
{
"award" : "Draper Prize",
"year" : 1993,
"by" : "National Academy of Engineering"
}
]
},
{
"_id" : ObjectId("52e060dee4d16089c04c02e9"),
"name" : {
"first" : "John",
"last" : "McCarthy"
},
"birth" : ISODate("1927-09-03T15:00:00Z"),
"death" : ISODate("2011-12-23T15:00:00Z"),
"contribs" : [
"Lisp",
"Artificial Intelligence",
"ALGOL"
],
"awards" : [
{
"award" : "Turing Award",
"year" : 1971,
"by" : "ACM"
},
{
"award" : "Kyoto Prize",
"year" : 1988,
"by" : "Inamori Foundation"
},
{
"award" : "National Medal of Science",
"year" : 1990,
"by" : "National Science Foundation"
}
]
},
{
"_id" : 3,
"name" : {
"first" : "Grace",
"last" : "Hopper"
},
"title" : "Rear Admiral",
"birth" : ISODate("1906-12-08T15:00:00Z"),
"death" : ISODate("1991-12-31T15:00:00Z"),
"contribs" : [
"UNIVAC",
"compiler",
"FLOW-MATIC",
"COBOL"
],
"awards" : [
{
"award" : "Computer Sciences Man of the Year",
"year" : 1969,
"by" : "Data Processing Management Association"
},
{
"award" : "Distinguished Fellow",
"year" : 1973,
"by" : " British Computer Society"
},
{
"award" : "W. W. McDowell Award",
"year" : 1976,
"by" : "IEEE Computer Society"
},
{
"award" : "National Medal of Technology",
"year" : 1991,
"by" : "United States"
}
]
},
{
"_id" : 4,
"name" : {
"first" : "Kristen",
"last" : "Nygaard"
},
"birth" : ISODate("1926-08-26T15:00:00Z"),
"death" : ISODate("2002-08-09T15:00:00Z"),
"contribs" : [
"OOP",
"Simula"
],
"awards" : [
{
"award" : "Rosing Prize",
"year" : 1999,
"by" : "Norwegian Data Association"
},
{
"award" : "Turing Award",
"year" : 2001,
"by" : "ACM"
},
{
"award" : "IEEE John von Neumann Medal",
"year" : 2001,
"by" : "IEEE"
}
]
},
{
"_id" : 5,
"name" : {
"first" : "Ole-Johan",
"last" : "Dahl"
},
"birth" : ISODate("1931-10-11T15:00:00Z"),
"death" : ISODate("2002-06-28T15:00:00Z"),
"contribs" : [
"OOP",
"Simula"
],
"awards" : [
{
"award" : "Rosing Prize",
"year" : 1999,
"by" : "Norwegian Data Association"
},
{
"award" : "Turing Award",
"year" : 2001,
"by" : "ACM"
},
{
"award" : "IEEE John von Neumann Medal",
"year" : 2001,
"by" : "IEEE"
}
]
}
]
>
- data를 save할 때는 다음과 같이 한다.
> db.bios.save(
{
name: { first: 'Guido', last: 'van Rossum'},
birth: new Date('Jan 31, 1956'),
contribs: [ 'Python' ],
awards: [
{
award: 'Award for the Advancement of Free Software',
year: 2001,
by: 'Free Software Foundation'
},
{
award: 'NLUUG Award',
year: 2003,
by: 'NLUUG'
}
]
}
)
> db.bios.find().toArray();
[
{
"_id" : 1,
"name" : {
"first" : "John",
"last" : "Backus"
},
"birth" : ISODate("1924-12-02T15:00:00Z"),
"death" : ISODate("2007-03-16T15:00:00Z"),
"contribs" : [
"Fortran",
"ALGOL",
"Backus-Naur Form",
"FP"
],
"awards" : [
{
"award" : "W.W. McDowell Award",
"year" : 1967,
"by" : "IEEE Computer Society"
},
{
"award" : "National Medal of Science",
"year" : 1975,
"by" : "National Science Foundation"
},
{
"award" : "Turing Award",
"year" : 1977,
"by" : "ACM"
},
{
"award" : "Draper Prize",
"year" : 1993,
"by" : "National Academy of Engineering"
}
]
},
{
"_id" : ObjectId("52e060dee4d16089c04c02e9"),
"name" : {
"first" : "John",
"last" : "McCarthy"
},
"birth" : ISODate("1927-09-03T15:00:00Z"),
"death" : ISODate("2011-12-23T15:00:00Z"),
"contribs" : [
"Lisp",
"Artificial Intelligence",
"ALGOL"
],
"awards" : [
{
"award" : "Turing Award",
"year" : 1971,
"by" : "ACM"
},
{
"award" : "Kyoto Prize",
"year" : 1988,
"by" : "Inamori Foundation"
},
{
"award" : "National Medal of Science",
"year" : 1990,
"by" : "National Science Foundation"
}
]
},
{
"_id" : 3,
"name" : {
"first" : "Grace",
"last" : "Hopper"
},
"title" : "Rear Admiral",
"birth" : ISODate("1906-12-08T15:00:00Z"),
"death" : ISODate("1991-12-31T15:00:00Z"),
"contribs" : [
"UNIVAC",
"compiler",
"FLOW-MATIC",
"COBOL"
],
"awards" : [
{
"award" : "Computer Sciences Man of the Year",
"year" : 1969,
"by" : "Data Processing Management Association"
},
{
"award" : "Distinguished Fellow",
"year" : 1973,
"by" : " British Computer Society"
},
{
"award" : "W. W. McDowell Award",
"year" : 1976,
"by" : "IEEE Computer Society"
},
{
"award" : "National Medal of Technology",
"year" : 1991,
"by" : "United States"
}
]
},
{
"_id" : 4,
"name" : {
"first" : "Kristen",
"last" : "Nygaard"
},
"birth" : ISODate("1926-08-26T15:00:00Z"),
"death" : ISODate("2002-08-09T15:00:00Z"),
"contribs" : [
"OOP",
"Simula"
],
"awards" : [
{
"award" : "Rosing Prize",
"year" : 1999,
"by" : "Norwegian Data Association"
},
{
"award" : "Turing Award",
"year" : 2001,
"by" : "ACM"
},
{
"award" : "IEEE John von Neumann Medal",
"year" : 2001,
"by" : "IEEE"
}
]
},
{
"_id" : 5,
"name" : {
"first" : "Ole-Johan",
"last" : "Dahl"
},
"birth" : ISODate("1931-10-11T15:00:00Z"),
"death" : ISODate("2002-06-28T15:00:00Z"),
"contribs" : [
"OOP",
"Simula"
],
"awards" : [
{
"award" : "Rosing Prize",
"year" : 1999,
"by" : "Norwegian Data Association"
},
{
"award" : "Turing Award",
"year" : 2001,
"by" : "ACM"
},
{
"award" : "IEEE John von Neumann Medal",
"year" : 2001,
"by" : "IEEE"
}
]
},
{
"_id" : ObjectId("52e0631ee4d16089c04c02eb"),
"name" : {
"first" : "Guido",
"last" : "van Rossum"
},
"birth" : ISODate("1956-01-30T15:00:00Z"),
"contribs" : [
"Python"
],
"awards" : [
{
"award" : "Award for the Advancement of Free Sof
tware",
"year" : 2001,
"by" : "Free Software Foundation"
},
{
"award" : "NLUUG Award",
"year" : 2003,
"by" : "NLUUG"
}
]
}
]
>
[mongodb] 강좌4 BigData MongoDB(NoSQL)
'database > mongodb' 카테고리의 다른 글
[mongodb] 강좌5 BigData MongoDB(NoSQL) (0) | 2014.03.03 |
---|---|
[mongodb] 강좌3 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 |