Posted by leafbird 트랙백 0 : 댓글 0

댓글을 달아 주세요

Mongo DB Tutorial

2011. 4. 5. 15:37 from 프로그래밍 팁/DB


출처 :

mongodb의 공식 사이트인 에도 튜토리얼 문서가 있지만 이게 더 내용이 간편하다.
간단하게 나중에 다시 볼 필요성이 있을 듯한데, 원본 페이지는 인터프리터 형식을 따라한 모양을 하고 있어서 매 단계마다 프롬프트에 next라고 입력해야 볼 수 있게 되어있다. 그래서 한 번에 긁어서 붙여 두었음.

이것 먼저 보고 공식 사이트 있는 것 보면 될 듯.  (공식 사이트 튜토리얼 : )

간만에 신기한 장난감을 만났군 ㅎ

This is a self-guided tutorial on MongoDB and the MongoDB shell.
The tutorial is simple, more or less a few basic commands to try.
To go directly to any part tutorial, enter one of the commands t0, t1, t2...t10
Otherwise, use 'next' and 'back'. Start by typing 'next' and pressing enter.

1. JavaScript Shell
The first thing to notice is that the MongoDB shell is JavaScript-based.
So you can do things like:
  a = 5;
  a * 10;
  for(i=0; i<10; i++) { print('hello'); };
Try a few JS commands; when you're ready to move on, enter 'next'

2. Documents
MongoDB is a document database. This means that we store data as documents,
which are similar to JavaScript objects. Here below are a few sample JS objects:
  var a = {age: 25};
  var n = {name: 'Ed', languages: ['c', 'ruby', 'js']};
  var student = {name: 'Jim', scores: [75, 99, 87.2]};
Create some documents, then enter 'next'

3. Saving
Here's how you save a document to MongoDB:{a: 99});

This says, "save the document '{a: 99}' to the 'scores' collection."
Go ahead and try it. Then, to see if the document was saved, try
Once you've tried this, type 'next'.

4. Saving and Querying
Try adding some documents to the scores collection:
  for(i=0; i<10; i++) {{a: i, exam: 5}) };

Try that, then enter
to see if the save succeeded. Since the shell only displays 10 results at time,
you'll need to enter the 'it' command to iterate over the rest.

(enter 'next' when you're ready)

5. Basic Queries
You've already tried a few queries, but let's make them more specific.
How about finding all documents where a == 2:
  db.scores.find({a: 2});

Or what about documents where a > 15?
  db.scores.find({a: {'$gt': 15}});

6. Query Operators
Query Operators:
$gt is one of many special query operators. Here are few others:
  $lt  - '<',   $lte - '<=',
  $gte - '>=',  $ne  - '!='
  $in - 'is in array',  $nin - '! in array'

db.scores.find({a: {'$in': [2, 3, 4]}});
db.scores.find({a: {'$gte': 2, '$lte': 4}});
Try creating some queries, then type 'next.'

7. Updates
Now create a couple documents like these for updating:{name: 'Johnny', languages: ['ruby', 'c']});{name: 'Sue', languages: ['scala', 'lisp']});
Make sure they were saved by called db.users.find()
Update the first document like so:
  db.users.update({name: 'Johnny'}, {name: 'Cash', languages: ['english']});

8. Update Operators
The previous update replaced the entire document, but MongoDB also
supports partial updates to documents. For example, you can set a value:
  db.users.update({name: 'Cash'}, {'$set': {'age': 50} });
You can also push and pull items from arrays:
  db.users.update({name: 'Sue'}, {'$pull': {'languages': 'scala'} });
  db.users.update({name: 'Sue'}, {'$push': {'languages': 'ruby'} });
Give these a try, check the results, and then enter 'next'.

9. Deleting data
To delete matching documents only, add a query selector to the remove method:
  db.users.remove({name: 'Sue'});
To delete everything from a collection:

10. Now go download it!
There's a lot more to MongoDB than what's presented in this tutorial.
Best thing is to go to the downloads page or to to check out the docs.
(You can also keep fiddling around here, but you'll be a bit limited.)

Posted by leafbird 트랙백 0 : 댓글 0

댓글을 달아 주세요