05-11 02:48
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[MongoDB] MongDB 기본명령어 본문

Data Science/MongoDB

[MongoDB] MongDB 기본명령어

cinema4dr12 2014. 1. 13. 23:52

Written by cinema4d

Create

"title", "content", "date" 키를 갖는 "post" 변수 생성 :

> post = {"title" : "My first blog post",
... "content" : "Getting started with MongDB",
... "date" : new Date()}

{
	"title" : "My fist blog post",
	"content" : "Getting started with MongDB",
	"date" : ISODate("2014-01-13T14:40:39.232Z")
}

insert 메써드를 이용하여 "post" 변수를 "blog" 콜렉션에 저장 :

> db.blog.insert(post)

find 메써드를 입력하면 "blog" 콜렉션에 저장된 값 확인 :

> db.blog.find()
{ "_id" : ObjectId("52d3fb75da84785bf64aaad9"), "title" : "My fist blog post", "content" : "Getting started with MongDB", "date" : ISODate("2014-01-13T14:40:39.232Z") } 


Read

하나의 도큐먼트만을 표시할 경우 :

> db.blog.findOne()

find 메써드는 도큐먼트를 모두 표시하며 shell에 최대 20개씩 표시된다.


Update

"post" 컬렉션에 "comments" 키 생성 :

> post.comments = []
[ ]
> post
{
	"title" : "My first blog post",
	"content" : "Getting Started with MongoDB",
	"date" : ISODate("2014-01-17T10:04:08.641Z"),
	"comments" : [ ]
}

"My first blog post"라는 타이틀을 가진 "post"로 도큐먼트 업데이트 : 

> db.blog.update({title : "My fist blog post"}, post)
> db.blog.findOne()
{
	"_id" : ObjectId("52d900298d2a9dc822dd0eea"),
	"title" : "My first blog post",
	"content" : "Getting Started with MongoDB",
	"date" : ISODate("2014-01-17T10:04:08.641Z"),
	"comments" : [ ]
}

 

Delete

"My first blog post" 타이틀의 컬렉션 삭제 :

> db.blog.remove({title : "My first blog post"})
> db.blog.findOne()
null


Comments