博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nodejs mongodb 查询要看的文章
阅读量:4617 次
发布时间:2019-06-09

本文共 2668 字,大约阅读时间需要 8 分钟。

数组很大多数情况下可以这样理解:每一个元素都是整个键的值.

db.users.findOne({"userName":"wyx","emails":"bbb@qq.com"})能匹配到

{

userName: 'wyx',

emails:[

'aaa@qq.com',

'bbb@qq.com',

'ccc@qq.com'

]

}

 

MongoDB高级查询

Node+Mongoose常用查询中文文档

 

推荐看这三篇 mongodb的查询,以及nodejs和mongoose联用的情况

看完mongoose的基本查询就没问题了

 

如何在mongodb中使用索引?

 

MongoDB组合索引的优化

非常好的文章,将索引,排序等问题的性能优劣取舍讲的很透彻

explain方法若出现BasicCursor可以视为警告,它意味着MongoDB将对数据集做一个完全的扫描。当数据集里包含上千万条信息时,这完全是行不通的。因此要考虑加上适当的索引

 

 

常用的关键字, count, distinct, group ....

 

 

 

mongoose中的2中操作方法,

callback and query returned

Queries

Documents can be retrieved through several static helper methods of .

Any  method which     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  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` fieldsPerson.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 errorparameter will contain an error document, and result will be null. If the query is successful, the errorparameter 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` fieldsquery.select('name occupation');// execute the query at a later timequery.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  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);

 

转载于:https://www.cnblogs.com/vincedotnet/p/3538337.html

你可能感兴趣的文章
netty与MQ使用心得
查看>>
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
查看>>
swoolefy PHP的异步、并行、高性能网络通信引擎内置了Http/WebSocket服务器端/客户端...
查看>>
Python学习笔记
查看>>
unshift()与shift()
查看>>
使用 NPOI 、aspose实现execl模板公式计算
查看>>
行为型模式:中介者模式
查看>>
How to Notify Command to evaluate in mvvmlight
查看>>
33. Search in Rotated Sorted Array
查看>>
461. Hamming Distance
查看>>
Python垃圾回收机制详解
查看>>
个人介绍
查看>>
mui搜索框 搜索点击事件
查看>>
最简单的三层实例【插入据
查看>>
设计模式学习笔记——Prototype原型模式
查看>>
pom.xml里有红叉报错的解决办法
查看>>
Perl last和next的用法区别
查看>>
Selenium 管理 Cookies
查看>>
exceptionfunction[LeetCode]Permutations
查看>>
bzoj 4595 激光发生器
查看>>