Mongo dbref附加字段在mongoshell中是不可见的。如何显示? [英] Mongo dbref additional fields are invisible in mongoshell. How to display them?

查看:177
本文介绍了Mongo dbref附加字段在mongoshell中是不可见的。如何显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:这个问题出现了Doctrine ODM,它使用DBRefs中的_doctrine_class_name字段,在Mongo shell(2.2.2)中是不可见的,当我们必须手动更新记录时,引起了相当的祸患。 >

示例:

  mongoshell>使用testdb; // for safety 
mongoshell> a = DBRef(layout_block,ObjectId(510a71fde1dc610965000005)); //创建一个dbref
mongoshell> a.hiddenfield =whatever//添加通常不在Doctrine的字段
mongoshell> a //查看它的内容,你不会看到hiddenfield
mongoshell> for(k in a){var val = a [k]; print(k +(+ typeof(val)+):+ val); } //你可以看到,如果你遍历它还有更多的东西
mongoshell> db.testcoll.save({ref:[a]})//你可以在一个集合
mongoshell> db.testcoll.findOne(); //通常你不会看到它

没有像下面的第三个命令(或MongoVue),如果您只使用find(),您将不会知道DBRef中有更多的内容。我没有找到任何可用的修饰符find()(尝试:toArray,tojson,printjson,toString,hex,base64,pretty,chatty,verbose,...)。



有没有人有方法在mongo shell中详细地显示DBRef内容?

解决方案

Mongo shell是 Mozilla SpiderMonkey (1.7?),具有非常裸的骨骼功能。



来自 MongoDB博客文章的建议外壳将在您的主目录中定义以下检查函数 .mongorc.js p>

  function inspect(o,i){
if(typeof i ==undefined){
i = ;
}
if(i.length> 50){
return[MAX ITERATIONS];
}
var r = [];
for(var p in o){
var t = typeof o [p];
r.push(i +\+ p +\(+ t +)=>+
(t ==object? + inspect(o [p],i +):o [p] +));
}
return r.join(i +\\\
);
}

此外,您可以重新定义DBRef.toString函数,如下所示:

  DBRef.prototype.toString = function(){
var r = ['$ ref:'+ tojson $ ref),'$ id:'+ tojson(this。$ id)];
var o = this;
for(var p in o){
if(p!=='$ ref'&& p!=='$ id'){
var t = typeof o [ p];
r.push(''''+ p +'('+ t +'):'+
(t =='object'?'object:{...}':o [ p] +''));
}
}
return'DBRef('+ r.join(',')+')';
};


Background: This problem came up with Doctrine ODM, that uses a _doctrine_class_name field in DBRefs that is invisible in the Mongo shell (2.2.2) and caused quite a culprit, when we had to update a record manually.

Example:

mongoshell> use testdb; // for safety
mongoshell> a = DBRef("layout_block", ObjectId("510a71fde1dc610965000005")); // create a dbref
mongoshell> a.hiddenfield = "whatever" // add a field that's normally not there like Doctrine does
mongoshell> a // view it's contents, you won't see hiddenfield
mongoshell> for (k in a) { var val = a[k]; print( k + "(" + typeof(val) + "): " + val ); } // you can see that there's more if you iterate through it
mongoshell> db.testcoll.save({ref: [ a ]}) // you can have it in a collection
mongoshell> db.testcoll.findOne(); // and normally you won't see it

Without an iteration like the third command from below (or MongoVue), you won't ever know there's more in a DBRef if you simply use find(). I have not found any usable modifier for find()(tried: toArray, tojson, printjson, toString, hex, base64, pretty, chatty, verbose, ...).

Has anybody got a method to display DBRef contents verbosely in mongo shell?

解决方案

The Mongo shell is an extension of Mozilla SpiderMonkey (1.7?) and has pretty bare bones functionality.

The suggestion from a MongoDB blog post on the shell is to define the following inspect function in .mongorc.js in your home directory

function inspect(o, i) {
    if (typeof i == "undefined") {
        i = "";
    }
    if (i.length > 50) {
        return "[MAX ITERATIONS]";
    }
    var r = [];
    for (var p in o) {
        var t = typeof o[p];
        r.push(i + "\"" + p + "\" (" + t + ") => " + 
              (t == "object" ? "object:" + inspect(o[p], i + "  ") : o[p] + ""));
    }
    return r.join(i + "\n");
}

Additionally you can redefine the DBRef.toString function as something like:

DBRef.prototype.toString = function () {
    var r = ['"$ref": ' + tojson(this.$ref), '"$id": ' + tojson(this.$id)];
    var o = this;
    for (var p in o) {
        if (p !== '$ref' && p !== '$id') {
            var t = typeof o[p];
            r.push('"' + p + '" (' + t + ') : ' + 
                (t == 'object' ? 'object: {...}' : o[p] + ''));
        }
    }
    return 'DBRef(' + r.join(', ') + ')';
};

这篇关于Mongo dbref附加字段在mongoshell中是不可见的。如何显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆