【问题标题】:Is it possible to write to mongodb console in javascript execution?是否可以在 javascript 执行中写入 mongodb 控制台?
【发布时间】:2011-12-21 10:00:16
【问题描述】:

我正在学习 mongodb 的 map-reduce 功能。我的第一次测试没有按预期进行,我想知道它是如何工作的。

是否有任何方法可以从 javascript 函数写入 mongodb 控制台以便我可以检查它?

我尝试了console.log("..."),但它不起作用。

如果没有办法,我稍后会询问我的测试。

【问题讨论】:

    标签: javascript mongodb


    【解决方案1】:

    您必须使用“print( "anything .." )”或printjson 来显示对象。

    andrey@andrey:~$ mongo
    MongoDB shell version: 2.0.2
    connecting to: test
    > object = { "name" : "any name .." , "key" : "value" }
    { "name" : "any name ..", "key" : "value" }
    > printjson ( object )
    { "name" : "any name ..", "key" : "value" }
    > print ( "hello world" )
    hello world
    >
    

    【讨论】:

    • 我在 Mac 上,这行得通。如果它不起作用,那是因为你做错了其他事情。仔细检查您的文件路径、权限等。
    【解决方案2】:

    我猜你需要从 map/reduce 函数中将 debug 消息插入到一些日志集合中:

    var map = function() {
      //some staff here
    };
    
    var reduce = function(key, values) {
      db.mr_logs.insert({message: "Message from reduce function"});
      //some staff here
    };
    
    
    res = db.items.mapReduce(map, reduce,{ query : {}, out : 'example1' })
    

    在此之后,您可以在mr_logs 集合中找到您的调试结果。

    db.mr_logs.find();
    

    至于print,当您使用 map 或 reduce 函数时,它似乎不会将输出打印到控制台。

    【讨论】:

      【解决方案3】:

      在 map-reduce 环境中有一个超级简单的解决方法。

      How to get print output for debugging map/reduce in Mongoid

      【讨论】:

        【解决方案4】:

        你可以像这样写函数/对象的名称:

        >fn = function (){return12;}
        >fn
        function (){return12;}
        >
        

        在这里试试:http://try.mongodb.org/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-04
          • 2011-09-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-03
          • 1970-01-01
          相关资源
          最近更新 更多