日期:2014-05-16  浏览次数:20440 次

mongodb -- Tutorial
Tutorial
Running MongoDB
Getting A Database Connection
Dynamic Schema ("Schema Free")
Inserting Data into A Collection
Accessing Data From a Query
Specifying What the Query Returns
findOne() - Syntactic Sugar
Limiting the Result Set via limit()
More Help
What Next
Running MongoDB
First, run through the Quickstart guide for your platform to get up and running.

Getting A Database Connection
Let's now try manipulating the database with the database shell . (We could perform similar operations from any programming language using an appropriate driver.  The shell is convenient for interactive and administrative use.)

Start the MongoDB JavaScript shell with:

# 'mongo' is shell binary. exact location might vary depending on
# installation method and platform
$ bin/mongo
By default the shell connects to database "test" on localhost.  You then see:

MongoDB shell version: <whatever>
url: test
connecting to: test
type "help" for help
>
"connecting to:" tells you the name of the database the shell is using. To switch databases, type:

> use mydb
switched to db mydb
Switching to a database with the use command won't immediately create the database - the database is created lazily the first time data is inserted. This means that if you use a database for the first time it won't show up in the list provided by `show dbs` until data is inserted.

To see a list of handy commands, type help.

Tip for Developers with Experience in Other Databases
You may notice, in the examples below, that we never create a database or collection. MongoDB does not require that you do so. As soon as you insert something, MongoDB creates the underlying collection and database. If you query a collection that does not exist, MongoDB treats it as an empty collection.
Dynamic Schema ("Schema Free")
MongoDB has databases, collections, and indexes much like a traditional RDBMS. In some cases (databases and collections) these objects can be implicitly created, however once created they exist in a system catalog (db.systems.collections, db.system.indexes).

Collections contain (BSON) documents. Within these documents are fields. In MongoDB there is no predefinition of fields (what would be columns in an RDBMS). There is no schema for fields within documents – the fields and their value datatypes can vary. Thus there is no notion of an "alter table" operation which adds a "column". In practice, it is highly common for a collection to have a homogenous structure across documents; however this is not a requirement. This flexibility means that schema migration and augmentation are very easy in practice - rarely will you need to write scripts which perform "alter table" type operations. In addition to making schema migration flexible, this facility makes iterative software development atop the database easier.

Inserting Data into A Collection
Let's create a test collection and insert some data into it. We will create two objects, j and t, and then save them in the collection things.

In the following examples, '>' indicates commands typed at the shell prompt.

> j = { name : "mongo" };
{"name" : "mongo"}
> t = { x : 3 };
{ "x" : 3  }
> db.things.save(j);
> db.things.save(t);
> db.things.find();
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
>
A few things to note :

We did not predefine the collection. The database creates it automatically on the first insert.
The documents we store can have different fields - in fact in this example, the documents have no common data elements at all. In practice, one usually stor