2016-12-04 83 views
0

虽然蒙戈外壳里面,试想一下,一个名为“样本”,其具有以下数据的收集,Mongo DB shell,remove命令不返回游标来解释(命令)?

{ "_id" : ObjectId("58437bd02e0a90d8318cfab1"), "a" : 1, "b" : 1} 
{ "_id" : ObjectId("58437bf02e0a90d8318cfab3"), "a" : 2, "b" : 1 } 
{ "_id" : ObjectId("58437bf32e0a90d8318cfab4"), "a" : 2, "b" :2} 

db.sample.explain().remove({a:1})的作品,但 db.sample.remove({a:1}).explain()不起作用,为什么呢?

在视频课程(University @ MongoDB)中声明删除不返回光标,这就是为什么db.sample.remove({a:1}).explain()不会起作用。但是我不明白,如果remove没有返回光标,那么db.sample.explain().remove({a:1})命令中的说明对象怎样才能处理查询并返回Winning计划等。

回答

0

为了弄清楚这一点,我们使用在mongo shell中输入函数名称将打印函数定义。

使用explain以前remove开始:

> db.sample.explain 
function (verbosity) { 
    return new Explainable(this, verbosity); 
} 

所以explain返回Explainable。但是Explainable是什么?

> Explainable 
function constructor(collection, verbosity) { 
    ... code omitted 
    this.find = function() { ... } 
    ... 
    this.remove = function() { ... } 

所以explain返回具有在其内部的一个定义remove(和所有其他查询可解释)的对象,从而有效地取代了常规remove命令而不在前面解释。从explain.remove的定义可以看出,魔术出现在ConvertToExplainCmd之内,导致执行explain命令而不是实际的remove命令。

现在对于remove第一前explain

> db.sample.remove 
function (t, justOne) { 
    ... 
    return result; 
} 

在这种情况下,返回result没有定义一个名为explain这样做db.sample.remove({a:1}).explain()不能正常工作的功能。

+0

感谢洛根,我现在很清楚:) 下一次,我将不得不深入了解功能。 – ManojKumar