Mongodb Study Notes 04, mongodb Study Notes
Cursor
- Var cursor = db. collectionName. find () create a cursor
- Whether cursor. hasNext () has the next element
- Cursor. next () retrieves the next element, for example, while (cursor. hasNext () {printjson (cursor. next ());}
Cursor. forEach (function) loops such as cursor. forEach (function (obj) {printjson (obj );})
Implement Paging
Cursor. skip (n) skips n rows
Cursor. limit (n) shows n rows
For example, 10 entries per page are displayed on page 1.
Var cursor = db. stu. find (). skip (5*9). limit (10 );
Convert to array
Cursor. toArray ()
Index
- Cursor. explain () view the query plan
- Db. collectionName. ensureIndex ({xxx}) to create a single column index, for example, db. stu. ensureIndex ({age: 1}) to age in ascending order
- Db. collectionName .. getIndexes () view Indexes
- Db. collectionName. dropIndex () Delete All indexes
- Db. collectionName. dropIndex ({xxx}) delete Index
Db. collectionName. reIndex () re-Indexing
Create multi-column Indexes
Db. stu. ensureIndex ({age: 1, stu_id:-1 })
Create a sub-document index
Db. stu. ensureIndex ({father. age: 1 })
Create a unique index
Db. stu. ensureIndex ({stu_id: 1}, {unique: true })
Create a hash Index
Db. stu. ensureIndex ({name: 'hashed '})