1

背景: この問題は、Mongo シェル (2.2.2) では見えない DBRefs の _doctrine_class_name フィールドを使用する Doctrine ODM で発生し、レコードを手動で更新する必要があったときにかなりの問題を引き起こしました。

例:

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

以下の 3 番目のコマンド (または MongoVue) のような反復がなければ、単純に find() を使用しただけでは、DBRef にそれ以上のものがあることを知ることはできません。find() の使用可能な修飾子が見つかりませんでした (試した: toArray、tojson、printjson、toString、hex、base64、pretty、chatty、verbose、...)。

DBRef の内容を mongo シェルで詳細に表示する方法を持っている人はいますか?

4

1 に答える 1

2

Mongo シェルはMozilla SpiderMonkey (1.7?) の拡張であり、必要最小限の機能を備えています。

シェルに関する MongoDB ブログ投稿からの提案は、ホーム ディレクトリに次のinspect関数を定義することです。.mongorc.js

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");
}

さらに、DBRef.toString 関数を次のように再定義できます。

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(', ') + ')';
};
于 2013-01-31T19:27:24.907 に答える