mongodb操作:利用javaScript封装db.collection.find()后可调用函数源码解读
{
"_mongo" : connection to YOURIP:27017{ SSL: { sslSupport: false, sslPEMKeyFile: "" } }{ SSH: { host: "", port: 22, user: "", password: "", publicKey: { publicKey: "", privateKey: "", passphrase: "" }, currentMethod: 0 } },
"_db" : mazhan_log,
"_collection" : YOURCOLLECTION,
"_ns" : "YOURCOLLECTION",
"_query" : { },
"_fields" : null,
"_limit" : 0,
"_skip" : 0,
"_batchSize" : 0,
"_options" : 0,
"_cursor" : { },
"_numReturned" : 50,
"_special" : false,
"_cursorSeen" : 50,
"help" : function () {
print("find() modifiers");
print("\t.sort( {...} )");
print("\t.limit( n )");
print("\t.skip( n )");
print("\t.count() - total # of objects matching query, ignores skip,limit");
print("\t.size() - total # of objects cursor would return, honors skip,limit");
print("\t.explain([verbose])");
print("\t.hint(...)");
print("\t.addOption(n) - adds op_query options -- see wire protocol");
print("\t._addSpecial(name, value) - http://dochub.mongodb.org/core/advancedqueries#AdvancedQueries-Metaqueryoperators");
print("\t.batchSize(n) - sets the number of docs to return per getMore");
print("\t.showDiskLoc() - adds a $diskLoc field to each returned object");
print("\t.min(idxDoc)");
print("\t.max(idxDoc)");
print("\nCursor methods");
print("\t.toArray() - iterates through docs and returns an array of the results");
print("\t.forEach( func )");
print("\t.map( func )");
print("\t.hasNext()");
print("\t.next()");
print("\t.objsLeftInBatch() - returns count of docs left in current batch (when exhausted, a new getMore will be issued)");
print("\t.count(applySkipLimit) - runs command at server");
print("\t.itcount() - iterates through documents and counts them");
},
"clone" : function () {
var q = new DBQuery(this._mongo, this._db, this._collection, this._ns, this._query, this._fields, this._limit, this._skip, this._batchSize, this._options);
q._special = this._special;
return q;
},
"_ensureSpecial" : function () {
if (this._special) {
return;
}
var n = {query:this._query};
this._query = n;
this._special = true;
},
"_checkModify" : function () {
if (this._cursor) {
throw "query already executed";
}
},
"_exec" : function () {
if (!this._cursor) {
assert.eq(0, this._numReturned);
this._cursor = this._mongo.find(this._ns, this._query, this._fields, this._limit, this._skip, this._batchSize, this._options);
this._cursorSeen = 0;
}
return this._cursor;
},
"limit" : function (limit) {
this._checkModify();
this._limit = limit;
return this;
},
"batchSize" : function (batchSize) {
this._checkModify();
this._batchSize = batchSize;
return this;
},
"addOption" : function (option) {
this._options |= option;
return this;
},
"skip" : function (skip) {
this._checkModify();
this._skip = skip;
return this;
},
"hasNext" : function () {
this._exec();
if (this._limit > 0 && this._cursorSeen >= this._limit) {
return false;
}
var o = this._cursor.hasNext();
return o;
},
"next" : function () {
this._exec();
var o = this._cursor.hasNext();
if (o) {
this._cursorSeen++;
} else {
throw "error hasNext: " + o;
}
var ret = this._cursor.next();
if (ret.$err && this._numReturned == 0 && !this.hasNext()) {
throw "error: " + tojson(ret);
}
this._numReturned++;
return ret;
},
"objsLeftInBatch" : function () {
this._exec();
var ret = this._cursor.objsLeftInBatch();
if (ret.$err) {
throw "error: " + tojson(ret);
}
return ret;
},
"readOnly" : function () {
this._exec();
this._cursor.readOnly();
return this;
},
"toArray" : function () {
if (this._arr) {
return this._arr;
}
var a = [];
while (this.hasNext()) {
a.push(this.next());
}
this._arr = a;
return a;
},
"count" : function (applySkipLimit) {
var cmd = {count:this._collection.getName()};
if (this._query) {
if (this._special) {
cmd.query = this._query.query;
} else {
cmd.query = this._query;
}
}
cmd.fields = this._fields || {};
if (applySkipLimit) {
if (this._limit) {
cmd.limit = this._limit;
}
if (this._skip) {
cmd.skip = this._skip;
}
}
var res = this._db.runCommand(cmd);
if (res && res.n != null) {
return res.n;
}
throw "count failed: " + tojson(res);
},
"size" : function () {
return this.count(true);
},
"countReturn" : function () {
var c = this.count();
if (this._skip) {
c = c - this._skip;
}
if (this._limit > 0 && this._limit < c) {
return this._limit;
}
return c;
},
"itcount" : function () {
var num = 0;
while (this.hasNext()) {
num++;
this.next();
}
return num;
},
"length" : function () {
return this.toArray().length;
},
"_addSpecial" : function (name, value) {
this._ensureSpecial();
this._query[name] = value;
return this;
},
"sort" : function (sortBy) {
return this._addSpecial("orderby", sortBy);
},
"hint" : function (hint) {
return this._addSpecial("$hint", hint);
},
"min" : function (min) {
return this._addSpecial("$min", min);
},
"max" : function (max) {
return this._addSpecial("$max", max);
},
"showDiskLoc" : function () {
return this._addSpecial("$showDiskLoc", true);
},
"readPref" : function (mode, tagSet) {
var readPrefObj = {mode:mode};
if (tagSet) {
readPrefObj.tags = tagSet;
}
return this._addSpecial("$readPreference", readPrefObj);
},
"forEach" : function (func) {
while (this.hasNext()) {
func(this.next());
}
},
"map" : function (func) {
var a = [];
while (this.hasNext()) {
a.push(func(this.next()));
}
return a;
},
"arrayAccess" : function (idx) {
return this.toArray()[idx];
},
"comment" : function (comment) {
var n = this.clone();
n._ensureSpecial();
n._addSpecial("$comment", comment);
return this.next();
},
"explain" : function (verbose) {
var n = this.clone();
n._ensureSpecial();
n._query.$explain = true;
n._limit = Math.abs(n._limit) * -1;
var e = n.next(); function cleanup(obj) {
if (typeof obj != "object") {
return;
}
delete obj.allPlans;
delete obj.oldPlan;
if (typeof obj.length == "number") {
for (var i = 0; i < obj.length; i++) {
cleanup(obj[i]);
}
}
if (obj.shards) {
for (var key in obj.shards) {
cleanup(obj.shards[key]);
}
}
if (obj.clauses) {
cleanup(obj.clauses);
}
} if (!verbose) {
cleanup(e);
}
return e;
},
"snapshot" : function () {
this._ensureSpecial();
this._query.$snapshot = true;
return this;
},
"pretty" : function () {
this._prettyShell = true;
return this;
},
"shellPrint" : function () {
try {
var start = (new Date).getTime();
var n = 0;
while (this.hasNext() && n < DBQuery.shellBatchSize) {
var s = this.next();
print(s);
n++;
}
if (typeof _verboseShell !== "undefined" && _verboseShell) {
var time = (new Date).getTime() - start;
print("Fetched " + n + " record(s) in " + time + "ms");
}
if (this.hasNext()) {
___it___ = this;
} else {
___it___ = null;
}
} catch (e) {
print(e);
}
},
"toString" : function () {
return "DBQuery: " + this._ns + " -> " + tojson(this._query);
}
}
先带源码裸奔, 解读注释部分随后补充。
其实js代码很好读的,嘿嘿!!!
mongodb操作:利用javaScript封装db.collection.find()后可调用函数源码解读的更多相关文章
- 【JavaScript游戏开发】使用HTML5+Canvas+JavaScript 封装的一个超级马里奥游戏(包含源码)
这个游戏基本上是建立在JavaScript模块化的开发基础上进行封装的,对游戏里面需要使用到的游戏场景进行了封装,分别实现了Game,Sprite,enemy,player, base,Animati ...
- Alamofire源码解读系列(九)之响应封装(Response)
本篇主要带来Alamofire中Response的解读 前言 在每篇文章的前言部分,我都会把我认为的本篇最重要的内容提前讲一下.我更想同大家分享这些顶级框架在设计和编码层次究竟有哪些过人的地方?当然, ...
- node.js连接MongoDB数据库,db.collection is not a function完美解决
解决方法一. mongodb数据库版本回退: 这个错误是出在mongodb的库中,在nodejs里的写法和命令行中的写法不一样,3.0的api已经更新和以前的版本不不一样,我们在npm中没指定版本号的 ...
- mongodb初始化并使用node.js实现mongodb操作封装
mongodb的下载只要在https://www.mongodb.com/网站就能够下载 下载后安装只用一直点next就可以,注意最好使用默认路径安装到C盘,然后在任意位置建立一个文件夹用于储存你的数 ...
- 支持Json进行操作的Javascript类库TAFFY DB
前段时间工作中用到Json数据,希望将一些简单的增删改查放到客户端来做,这样也能减少服务器端的压力.分别查找了几个可以对Json进行操作的javascript 类库,最终选定了TAFFY DB.原因如 ...
- mongoDB数据库插入数据时报错:db.collection is not a function
nodejs连接mongodb插入数据时,发现mongoDB报错:db.collection is not a function.解决方法: 1.npm下载mongodb2.x.x版本替换3.x.x ...
- mongodb的db.collection is not function
mongodb的3.0版本之前: 如2.3版本,可以直接使用db调用collection来操作数据 但在3.0版本以上,会报错:db.collection is not a function 3.0版 ...
- jdbc操作mysql(四):利用反射封装
前言 有了前面利用注解拼接sql语句,下面来看一下利用反射获取类的属性和方法 不过好像有一个问题,数据库中的表名和字段中带有下划线该如何解决呢 实践操作 工具类:获取connection对象 publ ...
- jdbc操作mysql(三):利用注解封装
案例五:利用注解封装 重复步骤 我们使用jdbc操作mysql时发现,操作不同表中数据,所写的方法基本相同:比如我们根据id向用户表添加数据,根据id删除商品表的数据,或者查询所有数据并用list集合 ...
随机推荐
- Install Orace 11g on Solaris 10 Sparc 64 bit
昨天有一个客户端安装11g数据库.整个安装过程和一些遇到的问题是一个创纪录.共享. 由于客户不能使用自己的机器远程连接到server,意通过U盘.移动硬盘等拷贝不论什么文件.因此一些记录内容无法做到非 ...
- 遗传算法解决旅行商问题(TSP)
这次的文章是以一份报告的形式贴上来,代码只是简单实现,难免有漏洞,比如循环输入的控制条件,说是要求输入1,只要输入非0就行.希望会帮到以后的同学(*^-^*) 一.问题描述 旅行商问题(Traveli ...
- hdu1381 Crazy Search(hash map)
题目意思: 给出一个字符串和字串的长度,求出该字符串的全部给定长度的字串的个数(不同样). 题目分析: 此题为简单的字符串哈hash map问题,能够直接调用STL里的map类. map<str ...
- 【本·伍德Lua专栏】补充的基础09:使用table.concat将一个大的字符串
近期2天都没有写新的文章了.主要是近期的内容没有特别有意思的. 之前的协同程序也临时没有感觉到特别适用的地方.今天在看数据结构的部分,也是没多大意思(不代表没用). 但是突然发现了一个有意思的地方,那 ...
- SQL Server 2008 R2 性能计数器详细列表(四)
原文:SQL Server 2008 R2 性能计数器详细列表(四) SQL Server Latches 对象: 监视称为闩锁的内部 SQL Server 资源锁.通过监视闩锁来确定用户活动和资源使 ...
- Fiddler工具的基本功能(转)
Fiddler是一款用于网页数据分析,抓取的工具,里面集成了对网页强大的功能外,还可以通过设置,使其对手机的数据也可以进行抓取 Fiddler的原理是: 通过在客户端和服务器之间创建一个代理服务器来对 ...
- 024找到二维阵列(keep it up)
剑指offer在标题中:http://ac.jobdu.com/problem.php? pid=1384 题目描写叙述: 在一个二维数组中,每一行都依照从左到右递增的顺序排序.每一列都依照从上到下递 ...
- Cocos2d-X字体
Cocos2d-X顺便文本显示在以下三个: CCLabelTTF: 使用系统字体.每一个字符串会生成一个纹理.显示效率比較低下,适合不变化的文字 CCLabelAtlas: 使用NodeAtlas优化 ...
- 数据库性能监测工具——SQL Server Profiler
使用SQL Server Profiler 进行sql监控需要一些设置: 其他的就是进行分析了~ 清除SQL SERVER缓存 常用的方法: DBCC DROPCLEANBUFFERS 从缓冲池中删除 ...
- java数据结构系列——排列(2):有序阵列
package Array; /** * 对数组排序.当添加到阵列保持有序数组元素: * @author wl * */ public class MyOrderArray { private lon ...