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

[经验分享] MongoDB 一些记录

[复制链接]

尚未签到

发表于 2015-7-8 11:28:12 | 显示全部楼层 |阅读模式
  Queries in MongoDB:
db.collection.find()
db.collection.findOne()
db.collection.find( ,  )
db.collection.findOne( ,  )

Operators:
$or, $lt, $gt, $in, dot notation, $elemMatch

Query Document:

db.collection.find( {} )
db.collection.find()
db.collection.find( { type: "snacks" } )
db.collection.find( { type: { $in: [ 'food', 'snacks' ] } } )
db.collection.find( { type: 'food', price: { $lt: 9.95 } } )
db.collection.find( { $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ] } )
db.collection.find( { type: 'food', $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ] } )

Subdocuments:

db.collection.find({ producer: { company: "ABC123", address : "123 Street" }})
db.collection.find( { 'producer.company': 'ABC123' } )

Arrays:
db.collection.find( { tags: [ 'fruit', 'food', 'citrus' ] } )
the query matches all documents where the value of the field tags is an array and holds three elements, 'fruit', 'food', and 'citrus', in this order
db.collection.find( { tags: 'fruit' } )
the query matches all documents where the value of the field tags is an array that contains, as one of its elements, the element 'fruit'
db.collection.find( { 'tags.0' : 'fruit' } )
the query uses the dot notation to match all documents where the value of the tags field is an array whose first element equals 'fruit'
db.collection.find( { 'memos.0.by': 'shipping' } )
selects all documents where the memos contains an array whose first element (i.e. index is 0) is a subdocument with the field by with the value 'shipping'
db.collection.find( {'memos.by' ; 'shipping'} )
selects all documents where the memos field contains an array that contains at least one subdocument with the field by with the value 'shipping'
db.collection.find( { 'memos.memo': 'on time', 'memos.by': 'shipping' } )
query for documents where the value of the memos field is an array that has at least one subdocument that contains the field memo equal to 'on time' and the field by equal to 'shipping'
db.collection.find({memos : { $elemMatch : { memo : 'on time', by : 'shipping'}}})
same as above, just use "$elemMatch" operator

===============
文档  Document  集合Collection  子集合 blog.users  blog.contents   数据库: DB  // shell: show dbs; use mydb; show collections; db.mycollection;
  db.foo.insert({"name":"yy"}); // 每条插入的记录都会多一个属性 "_id", 每个集合的每个document对象都有唯一的"_id"。
  批量插入:插入 document数组 到一个collection中。
mongoimport 工具: 导入原始数据,从数据feed或者mysql导入。
删除文档:db.foo.remove(); //只是删除集合foo的文档,不删除集合本身,也不删除集合的索引。
db.foo.drop(); //删除整个foo集合。删除所有数据的话,这个更快。
db.foo.update(queryCnd, newDoc); //文档替换
  //数据的更新: . . . . . .
  //数据查询:
db.user.find() == db.user.find({})
db.user,.find({"name":"joe", "age":30}); //多条件联合查询
//指定返回的键,即映射, 无论如何默认_id总是返回,除非排除他 "_id":0
db.user.find({}, {"name":1,"age":1});
db.user.find({}, {"age":0});
db.user.find({}, {"name":1,"age":0});不允许,这个不能混用,但是 db.user.find({}, {"name":1,"_id":0});允许,_id好特殊啊
  db.user.find({"age":{"$gte":18, "lte":30}}); //$gte: great than and equal,  $lte: less than and equal
db.user.find( {"userId" : {"$in" : ["joe", 123]} }); // find user whose userid is "joe" or 123
db.raffle.find({"$or" : [{"ticket_no" : {"$in" : [390, 256, 757]} }, {"winner" : true}]}); // this is or usage
  // 使用and查询,第一个条件要尽量排除记录, 使用or,第一个条件要尽量匹配记录,这样效率才高。 mongodb是从左往右解析条件的。
  db.c.find({"z" : {"$in" : [null], "$exists" : true  } }); // 没有$eq操作符 ,所以用$in了
  db.user.find({"name" : /joe/i}); //查找名字为"Joe"(不区分大小写)的用户, 也可以匹配/joe/i 本身。
  // $where 查询, 效率问题, 另外一种复杂查询是 MapReduce方式
db.foo.insert({"apple" : 1, "banana" : 6, "peach" : 3});
db.foo.insert({"apple" : 1, "spinach" : 6, "peach" : 3});
  db.foo.find({"$where" : function(){
for(var cur in this){
for(var oth in this){
if(cur!=oth && this[cur] == this[other]){
return true;
}
}
}
return false;
}});
  // 游标 limit | skip | sort使用
var cursor = db.user.find();
var item = null;
while(cursor.hasNext()){
item = cursor.next();
// do stuff with item
}
  //skip 忽略大量数据是很慢滴
var cursor = db.user.find({"status" : 1}).sort({"name" : 1, "age" : -1}).limit(10).skip(30)
//游标默认超时==>>释放资源  //自定义超时时间
  //缩影Index 用来加速查询
db.user.ensureIndex({"name" : 1}) //name 的升序索引, 排序影响索引的缓存,尽量使的update/delete不会引起索引缓存的剧烈变化
//索引缺点: 每次插入、更新、和删除都会产生额外的开销,用于维持索引顺序。
//索引内嵌文档的键
db.user.ensureIndex("comments.date" : 1);
  //为排序创建索引,如果排序字段无索引,且数量巨大,内存排序无法实现,mongodb可能会报错
db.user.find(...).sort({"name" : 1});
db.user.ensureIndex({"name" : 1});
  //Index Name, 默认使用key1_sort1_key2_sort2..._keyN_sortN
db.user.ensureIndex({...}, {"name" : "userInamendex"})
  // 唯一性约束, unique index
db.user.ensureIndex({"name" : 1}, {"unique" : true});
db.user.ensureIndex({"name" : 1, "status" : 1}, {"unique" : true});
  //查询 删除索引
db.user.getIndexes();
db.user.dropIndex(indexName);
  //聚合
db.user.count();
db.user.count("status" : 1);
db.user.distinct("name"); //如果用distinct只能distinct一个字段
  //MapReduce  
emit(key, value) //按照key来分类,reduce 按照value,做出结果
db.bk.mapReduce(map,reduce,...);
  //~~~~~~~~~~~~~~~~~~~~~~~
数据库命令 shell命令 db.runCommand(...);
固定集合,类相似先进先出列表的序列化

运维网声明 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-84449-1-1.html 上篇帖子: 百度BAE 平台PHP对Mongodb的连接 下篇帖子: MongoDB学习笔记(六) MongoDB索引用法和效率分析(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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