http://www.cnblogs.com/refactor/archive/2012/07/30/2591344.html
數組很大多數情況下可以這樣理解:每一個元素都是整個鍵的值.
db.users.findOne({"userName":"wyx","emails":"bbb@qq.com"})能匹配到
{
userName: 'wyx',
emails:[
'aaa@qq.com',
'bbb@qq.com',
'ccc@qq.com'
]
}
MongoDB高級查詢
http://www.nonb.cn/blog/mongodb-advanced-queries.html
Node+Mongoose常用查詢中文文檔
http://www.nonb.cn/blog/nodejs-mongoose-query-chinaese.html
推薦看這三篇 mongodb的查詢,以及nodejs和mongoose聯用的情況
看完mongoose的基本查詢就沒問題了
如何在mongodb中使用索引?
http://www.cnblogs.com/huangxincheng/archive/2012/02/29/2372699.html
MongoDB組合索引的優化
非常好的文章,將索引,排序等問題的性能優劣取舍講的很透徹
http://www.csdn.net/article/2012-11-09/2811690-optimizing-mongodb-compound
explain方法若出現BasicCursor可以視為警告,它意味着MongoDB將對數據集做一個完全的掃描。當數據集里包含上千萬條信息時,這完全是行不通的。因此要考慮加上適當的索引
常用的關鍵字, count, distinct, group ....
http://www.cnblogs.com/huangxincheng/archive/2012/02/21/2361205.html
mongoose中的2中操作方法,
callback and query returned
http://mongoosejs.com/docs/queries.html
Queries
Documents can be retrieved through several static helper methods of models.
Any model method which involves specifying query conditions can be executed two ways:
When a callback
function:
- is passed, the operation will be executed immediately with the results passed to the callback.
- is not passed, an instance of Query is returned, which provides a special
QueryBuilder
interface for you.
Let's take a look at what happens when passing a callback
:
var Person = mongoose.model('Person', yourSchema); // Query
// find each person with a last name matching 'Ghost', selecting the `name` and `occupation` fields
Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host.
})
Here we see that the query was executed immediately and the results passed to our callback. All callbacks in Mongoose use the pattern: callback(error, result)
. If an error occurs executing the query, the error
parameter will contain an error document, and result
will be null. If the query is successful, the error
parameter will be null, and the result
will be populated with the results of the query.
Anywhere a callback is passed to a function in Mongoose, the callback follows the patterncallback(error, results)
.
Now let's look at what happens when no callback
is passed:
// find each person with a last name matching 'Ghost'
var query = Person.findOne({ 'name.last': 'Ghost' });
// selecting the `name` and `occupation` fields
query.select('name occupation');
// execute the query at a later time
query.exec(function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host.
})
An instance of Query was returned which allows us to build up our query. Taking this example further:
Person
.find({ occupation: /host/ })
.where('name.last').equals('Ghost')
.where('age').gt(17).lt(66)
.where('likes').in(['vaporizing', 'talking'])
.limit(10)
.sort('-occupation')
.select('name occupation')
.exec(callback);