Getting Started with mongoDB Development

After starting the mongo shell your session will use the test database for context, by default. At any time issue the following operation at the mongo to report the current database:

db

db returns the name of the current database.

Step 1: From the mongo shell, display the list of databases with the following operation:

> show dbs

Step 2: Switch to a new database named mydb with the following operation:

> use mydb 

Note: At this point, if you issue the show dbs operation again, it will not include mydb, because mongoDB will not create a new database until you insert data into that database.

Step 3: Create a document, named j, with the following JavaScript operation:

> j = { name : "mongo" }

Step 4:  Insert the j document into the collection things with the following operation:

> db.things.insert(j)

Note: When you insert the first document, the mongod will create both the mydb database and the things collection. 

Step 5: Confirm that the collection named things exists using the following operation:

> show collections

Step 6: Confirm that the documents exist in the collection things by issuing quey on the things collection:

> db.things.find()

This operation returns the following results. The ObjectId values will be unique:

{ "_id" : ObjectId("515feca6f2d89963b39e5c08"), "name" : "mongo" }

猜你喜欢

转载自jiyuanpeng.iteye.com/blog/1842702