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

mongodb官方文档摘要
By default, mongo looks for a database server listening on port 27017 on the localhost interface. To connect to a server on a different port or interface, use the --port and --host options.

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

At this point, if you issue the show dbs operation again, it will not include the mydb database. MongoDB will not permanently create a database until you insert data into that database.

用mongo连接上mongod实例时,默认使用test db,需要用use [db_name]命令,切换到目标数据库。除非确实插入文档,否则不会真正创建数据库,用show dbs命令暂时不会看见

When you access documents in a cursor using the array index notation (like c[6]), mongo first calls the cursor.toArray() method and loads into RAM all documents returned by the cursor. The index is then applied to the resulting array. This operation iterates the cursor completely and exhausts the cursor. For very large result sets, mongo may run out of available memory.

find()方法返回一个cursor,如果调用cursor[1],则会隐式调用cursor.toArray()函数,如果结果集很大,可能会OOM

With the findOne() method you can return a single document from a MongoDB collection. The findOne() method takes the same parameters as find(), but returns a document rather than a cursor.

findOne()方法确实地返回一个文档,而不是一个cursor

Collections are analogous to a table in relational databases.

芒果的collection是松散组织(no schema),允许插入完全没有关系的多条数据,唯一的关联是共享_id索引。但是这是不好的做法,还是推荐将有组织的文档插入同一个集合

Read operations, or queries, retrieve data stored in the database. In MongoDB, queries select documents from a single collection.

Queries specify criteria, or conditions, that identify the documents that MongoDB returns to the clients. A query may include a projection that specifies the fields from the matching documents to return. The projection limits the amount of data that MongoDB returns to the client over the network.

芒果的查询语句,包括criteria、projection、cursor modifier

MongoDB queries exhibit the following behavior:
?All queries in MongoDB address a single collection.
?You can modify the query to impose limits, skips, and sort orders.
?The order of documents returned by a query is not defined and is not necessarily consistent unless you specify a sort().
?Operations that modify existing documents (i.e. updates) use the same query syntax as queries to select documents to update.
?In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries.

MongoDB projections have the following properties:
?In MongoDB, the _id field is always included in results unless explicitly excluded.
?For fields that contain arrays, MongoDB provides the following projection operators: $elemMatch, $slice, $.
?For related projection functionality in the aggregation framework pipeline, use the $project pipeline stage.

To access the documents, you need to iterate the cursor. However, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents in the results.

在mongo shell中,如果find()方法没有赋值给一个var变量,则会默认取出前20条记录(调用20次cursor.next)

Because the cursor is not isolated during its lifetime, intervening write operations on a document may result in a cursor that returns a
document more than once if that document has changed.

cursor并不是isolated的,并不是DB某个时刻的快照

Some query operations are not selective. These operations cannot use indexes effectively or cannot use indexes at all.

The inequality operators $nin and $ne are not very selective, as they often match a large portion of the index. As a result, in most cases, a $nin or $ne query with an index may perform no bette