设为首页 收藏本站
查看: 1154|回复: 0

[经验分享] nodejs mongodb 查询要看的文章

[复制链接]

尚未签到

发表于 2015-7-8 04:00:06 | 显示全部楼层 |阅读模式
  http://www.iyunv.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.iyunv.com/huangxincheng/archive/2012/02/29/2372699.html
  

MongoDB组合索引的优化
  非常好的文章,将索引,排序等问题的性能优劣取舍讲的很透彻
  http://www.iyunv.com/article/2012-11-09/2811690-optimizing-mongodb-compound
  explain方法若出现BasicCursor可以视为警告,它意味着MongoDB将对数据集做一个完全的扫描。当数据集里包含上千万条信息时,这完全是行不通的。因此要考虑加上适当的索引
  
  
  常用的关键字, count, distinct, group ....
  http://www.iyunv.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 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` 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);
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-84162-1-1.html 上篇帖子: 让NodeJS更容易操作Mongodb数据库 下篇帖子: 关于内网linux系统如果安装nodejs,npm,express,mongodb,forever等
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表