惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园 - 司徒正美
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
小众软件
小众软件
T
Threatpost
Latest news
Latest news
J
Java Code Geeks
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Project Zero
Project Zero
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
人人都是产品经理
人人都是产品经理
S
Schneier on Security
T
The Blog of Author Tim Ferriss
V
V2EX
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
罗磊的独立博客
IT之家
IT之家
雷峰网
雷峰网
H
Help Net Security
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
C
Cybersecurity and Infrastructure Security Agency CISA
I
InfoQ
GbyAI
GbyAI
博客园 - 叶小钗
PCI Perspectives
PCI Perspectives
The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
H
Heimdal Security Blog
Spread Privacy
Spread Privacy
博客园_首页
A
About on SuperTechFans
T
Tailwind CSS Blog
The Register - Security
The Register - Security

博客园 - K!ngZ

常用正则 MongoDB学习笔记(9)--优化器 profile MongoDB学习笔记(8)--索引及优化索引 MongoDB学习笔记(7)--访问控制 MongoDB学习笔记(6)--数据备份数据恢复 MongoDB学习笔记(5)--数据导入导出mongoexport MongoDB学习笔记(4)--Capped Collection MongoDB学习笔记(3)--高级查询 MongoDB学习笔记(1)--了解MongoDB 远程文件抓取类 [转]BizTalk开发系列(三十四) Xpath SQL Server 2005 命令行实用工具 [转]雅虎公司C#笔试题 [转] OLE DB 访问接口 "SQLNCLI" 返回了消息 "未指定的错误"。 Asp.net中的日期处理函数 [转]正则表达式实现资料验证的技术总结 ajax乱码问题解决 [转]ASP.Net缓存总结 [转]预防按钮的多次点击【恶意刷新】
MongoDB学习笔记(2)--增删改查
K!ngZ · 2011-09-13 · via 博客园 - K!ngZ

查询记录

普通查询 

> var cursor = db.things.find();

> while (cursor.hasNext()) printjson(cursor.next());
   上面的例子显示了游标风格的迭代输出. hasNext()  函数告诉我们是否还有数据,  如果有则可以调用 next()  函数. 

当我们使用的是 JavaScript shell, 可以用到JS的特性, forEach 就可以输出游标了. 下面的例 子就是使用 forEach() 来循环输出: forEach() 必须定义一个函数供每个游标元素调用.

> db.things.find().forEach(printjson);

在 MongoDB shell 里, 我们也可以把游标当作数组来用:
   > var cursor = db.things.find();

> printjson(cursor[4]);

使用游标时候请注意占用内存的问题,  特别是很大的游标对象,  有可能会内该用迭代的方式来输出.  下面的示例则是把游标转换成真实的数组类型:  

> var arr = db.things.find().toArray();

> arr[5];
条件查询

> db.things.find({name:"mongo"}).forEach(printjson); 

> db.things.find({x:4,y:"abc"}).forEach(printjson);  

返回特定的元素 

> db.things.find({x:4}, {j:true}).forEach(printjson);

findOne()语法

printjson(db.things.findOne({name:"mongo"})); 

通过limit 限制结果集数量
> db.things.find().limit(3);

修改记录

  > db.things.update({name:"mongo"},{$set:{name:"mongo_new"}});

删除记录 

> db.things.remove({name:"mongo_new"});