设为首页 加入收藏

TOP

MongoDB数据库基础知识(二)
2015-11-21 01:26:17 来源: 作者: 【 】 浏览:1
Tags:MongoDB 数据库 基础知识
","cc3"],"$slice":-10}}})

$pop 从数组中删除一个元素 key : 1 从数据末尾开始删 key : -1 从头部开始删

 db.zy.update({"_id":1005},{"$pop":{"company":1}})

$pull 从数组中删除匹配的值

 db.zy.update({"_id":1005},{"$pull":{"company":"nh"}})

—-循环

 for( i=0;i<10;i++){ db.zy.insert({"_id":i,"name":i}) } 

注:当不插入_id时,mongodb会自动分配一个ObjectId
—-游标

 var cursor =db.zy.find(); var obj; while(cursor .hasNext()){ obj=cursor.next(); print(obj); }; var cursor =db.zy.find(); cursor .forEach(function(x){ print(x); })

–分页查询

 db.zy.find().limit(3); ---查前3条 db.zy.find().limit(3).skip(3); ---跳过前3条,查后面的3条

–排序

 db.zy.find().sort({"_id":-1}) --- 1为升序 -1 为降序 db.zy.find().sort({"_id":-1,"name":1}) ---第一个相同就按第二个排序

eg:

 db.zy.insert({ "_id":1006, "content":"今天天气怎么样??", "comments":[ {"comment":"好","count":0}, {"comment":"很好","count":0}, {"comment":"非常好","count":0}, ] })

—-通过数组下标访问

 db.zy.update({"_id":1006},{"$inc":{"comments.1.count":1}}) db.zy.update({"comments.comment":"很好"},{"$inc":{"comments.$.count":1}}) ----匹配所有的“很好”,让其对应的count+1 db.zy.update({"comments.comment":"非常好"},{"$set":{"comments.$.comment":"超级好"}}) ----匹配所有的“非常好”,让其对应的值变为超级好

—-MongoDB默认每次修改只修改一个文档,如果需要改多条满足的记录,需要在后面添加条件{multi:true}

 db.zy.update({"comments.comment":"很好"},{"$inc":{"comments.$.count":1}},{multi:true}) db.zy.update({"comments.comment":"很好"},{"$inc":{"comments.$.count":1}},false,true)

(5).数据类型:
null:{“x”:null}
boolean: {“x”:true}
数值: {“x”:3.14} {“x”:3} NumberInt(“3”) NumberLong(“3”)
字符串:{“x”:”hello”}
日期:{“x”:new Date()}
正则表达式:{“x”:/hello/ig}
数组:{“x”:[1,2,3]}
内嵌文档:{“x”:{“foo”:{bar}}}
对象id: {“x”:ObjectId()}
代码:{“x”:function(){} }

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇OracleEBSMRP模块之预测冲减 下一篇MySQL双主配置

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: