二、Mongodb实战之——Mongodb Shell实现简单插入和查询
2013-03-15 19:19
519 查看
insert
向集合中插入文档source code
function (obj, _allow_dot) { if (!obj) { throw "no object passed to insert!"; } if (!_allow_dot) { this._validateForStorage(obj); } if (typeof obj._id == "undefined" && !Array.isArray(obj)) { var tmp = obj; obj = {_id:new ObjectId}; for (var key in tmp) { obj[key] = tmp[key]; } } this._db._initExtraInfo(); this._mongo.insert(this._fullName, obj); this._lastID = obj._id; this._db._getExtraInfo("Inserted"); }
example:
>db.users.insert({"userName":"chjzh","pwd":"123"})
find
在集合中查询符合条件的文档,find()返回集合中所有文档source code
function (query, fields, limit, skip, batchSize, options) { return new DBQuery(this._mongo, this._db, this, this._fullName, this._massageObject(query), fields, limit, skip, batchSize, options || this.getQueryOptions()); }
example
>db.users.find({"userName":"chjzh"})
save
向集合中插入文档,与insert不同的是,若集合中存在重复的id则insert不做插入,而save则更改原来的内容为新内容source code
function (obj) { if (obj == null || typeof obj == "undefined") { throw "can't save a null"; } if (typeof obj == "number" || typeof obj == "string") { throw "can't save a number or string"; } if (typeof obj._id == "undefined") { obj._id = new ObjectId; return this.insert(obj); } else { return this.update({_id:obj._id}, obj, true); } }
example
>db.users.save({"userName":"chjzh","pwd":"111111"})
count
统计集合中满足条件的文档个数source code
function (x) { return this.find(x).count(); }
example
>db.users.count()
相关文章推荐
- 三、Mongodb实战之——Mongodb Shell实现简单更新
- hibernate实现的简单插入与查询
- 二叉树,建立,查询,删除,插入的简单实现
- 实战:Nodejs+Mongodb+Elasticsearch 实现简单的搜索
- mongodb简单的数据插入和查询
- cxf实现webservice简单插入,查询,和返回结构集
- [转]MongoDB插入和查询操作
- 分页查询简单实现(Freemarker+SpringMVC+Mybatis)
- spring data jpa封装specification实现简单风格的动态查询
- 简单选择、冒泡、插入排序思想及Java实现代码
- 关于使用js实现按期间查询的简单demo
- 使用c++driver实现mongodb空间数据查询
- 双向循环链表简单的插入、删除、修改以及查找功能的实现
- 分页查询的简单实现
- C# mongoDB.net driver 2.4.0实现上传下载文件及文件列表的多条件查询
- Android实战教程第三篇之简单实现拨打电话功能
- 【第十一章】 springboot + mongodb(简单查询)
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- Solr实战:使用Hue+Solr实现标签查询
- 简单的通过mysql来插入和查询数据