是否可以在 javascript 执行中写入 mongodb 控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8588006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to write to mongodb console in javascript execution?
提问by francadaval
I'm learning about map-reduce functionality of mongodb. My first tests doesn't work as I expected and I want to know how is it working.
我正在学习 mongodb 的 map-reduce 功能。我的第一个测试没有按预期工作,我想知道它是如何工作的。
Is there any way to write to mongodb console from javascript functionsso I can inspect it?
有什么方法可以从 javascript 函数写入 mongodb 控制台,以便我可以检查它?
I tried console.log("...")but it doesn't work.
我试过了,console.log("...")但没有用。
I will ask later about my tests if there isn't any way to do it.
如果没有任何方法可以做到,我稍后会询问我的测试。
回答by
You have to use 'print( "anything .." )' or printjsonto display objects.
您必须使用“ 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
>
回答by Andrew Orsich
I guess from map/reduce functions you need to insert your debugmessages into some logs collection:
我想从 map/reduce 函数中,您需要将调试消息插入到一些日志集合中:
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' })
After this you can find your debug results in mr_logscollection.
在此之后,您可以在mr_logs集合中找到您的调试结果。
db.mr_logs.find();
As for printit seems not printing output to console when you are in map or reduce functions.
至于print当您使用 map 或 reduce 函数时,它似乎不会将输出打印到控制台。
回答by Jerry Chen
there is a super easy workaround in map-reduce environment.
在 map-reduce 环境中有一个超级简单的解决方法。
回答by alessioalex
You can just write the name of the function / object like so:
您可以像这样写函数/对象的名称:
>fn = function (){return12;}
>fn
function (){return12;}
>
Try it here: http://try.mongodb.org/
在这里试试:http: //try.mongodb.org/

