【问题标题】:mongo console: how to print an element's internal typemongo 控制台:如何打印元素的内部类型
【发布时间】:2014-07-23 05:26:42
【问题描述】:

使用$type 和$match,我可以迭代地发现Mongo 认为我的数据具有哪些类型。使用$not,有时我可以更快地获得结果。

有没有办法直接project 文档中元素的类型? (我想我可以用 pymongo 得到它,但我还没有看过。) 谢谢!

【问题讨论】:

    标签: mongodb mapreduce mongodb-query pymongo


    【解决方案1】:

    你可以用 mapReduce 来做这件事。为了简单起见,我可以演示,但复杂的结构需要更多的实现才能真正递归地完成。

    所以给定一个文件。

    db.team.find()
    {
        "_id" : 50,
        "team_name" : "bulls",
        "players" : [
                {
                        "_id" : 100,
                        "player_name" : "Jokim"
                }
        ],
        "sub" : {
                "opt" : 1
        },
        "long" : NumberLong(123),
        "int" : 1,
        "bool" : false,
        "date" : ISODate("2014-06-03T01:42:01.016Z"),
        "unset" : null,
        "id" : ObjectId("538d36ccb88d0a9b6195ca66")
    }
    

    您可以运行这个“基本”映射器:

    db.team.mapReduce(
        function () {
    
            var obj = {};
    
            for ( var k in this ) {
                var type = ( this[k] != null ) 
                    ? this[k].constructor.toString() : null;
                var match = null;
                if (type != null)
                    match = type.match(/^function\s(\w+)/);
    
                obj[k] = ( match != null ) ? match[1]
                    : ( type != null ) ? 'Object' : null;
    
            }
    
            emit( this._id, obj );
    
        },
        function(){},
        { "out": { "inline": 1 } }
    )
    

    这会给你这样的输出:

    "results" : [
        {
            "_id" : 50,
            "value" : {
                "_id" : "Number",
                "team_name" : "String",
                "players" : "Array",
                "sub" : "Object",
                "long" : "NumberLong",
                "int" : "Number",
                "bool" : "Boolean",
                "date" : "Date",
                "unset" : null,
                "id" : "ObjectId"
            }
        }
    ],
    

    您可以根据需要使用它,递归地进入“数组”或“对象”(子文档)类型。但这是为给定字段/属性输出“类型”的一种方式。

    【讨论】:

    • 哇,多么整洁。这正是我想要的,但我在两个部分遇到了麻烦。 1.当我输入JS时 xx.constructor.toString();进入控制台我没有看到好的答案? ; 2.我得到了 OOM,我想我可以用另一个 map/reduce 步骤来修复它。 "map reduce failed:{ "errmsg" : "exception: memory map/reduce 数据太多", "code" : 13604, "ok" : 0"
    • @AnneTheAgile 似乎您正在遍历整个集合。您可以输出到另一个集合,或者以其他方式调整映射器和缩减器以收集“统计信息”,因为它在您的集合中存在哪些字段和“类型”。这实际上只是概念证明,表明您可以使用 JavaScript 以这种方式检查 BSON 类型。你的最终解决方案需要更多的工作。
    • @AnneTheAgile 几分钟后,我已经为您清理了“类型”名称。真的很简单的正则表达式。
    • 非常感谢您在几个方面对我进行了教育!在第二个匹配输出会删除字符串中任何指定的“常量”之前,我不知道/grok。这太有帮助了。自我注释; 1.Example Regex: IN= > match = "function Array() { [native code] }".match(/^function\s(\w+)/); OUT= [ "函数数组", "数组" ] ; 2.Neil原来的循环很简单:obj[k] = this[k].constructor.toString();
    • 3.MongoDB 对 map-reduce 的 RAM 限制可以通过输出到集合轻松修复,blog.evilmonkeylabs.com/2011/01/27/MongoDB-1_8-MapReduceblog.mongodb.org/post/3903149313/mongodb-1-8-released
    猜你喜欢
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    相关资源
    最近更新 更多