简体   繁体   English

MongoDB从控制台运行javaScript文件并输出

[英]MongoDB run javaScript file from console and output

I am trying to create some mongoDB script (files upload to collections, find, create new collections etc). 我正在尝试创建一些mongoDB脚本(文件上传到集合,查找,创建新集合等)。 Though get confused. 虽然感到困惑。 When I run in console: 当我在控制台中运行时:

> use importCSV    
> db.people.find().pretty()

I get documents from collection on my screen, though when I run load command 我在运行load命令时从屏幕上的集合中获取文档

> load('e:/work/parse/script.js')

i get output 我得到输出

true

Here javaScript file list 这里的javaScript文件列表

db = db.getSiblingDB('importCSV');
db.people.find().pretty();

I do it for debug purpose, so I create javaScript line by line to get what I want, and I need to see step by step some commands output. 我这样做是出于调试目的,因此我逐行创建javaScript以获取所需的内容,并且需要逐步查看一些命令输出。 If I put to javaScript file command like this 如果我把这样的javaScript文件命令

print('Print from javaScript file');

it prints to console without any problems. 它可以打印到控制台,没有任何问题。

Why I get "true" when run from file instead of console output, and how to get list of documents printed when run from javaScript file? 为什么从文件而不是控制台输出运行时得到“ true”,以及从javaScript文件运行时如何获取打印的文档列表?

Thanks 谢谢

This is an expected behaviour. 这是预期的行为。 You need to iterate through the cursor and print each document explicitly using a .forEach loop because you are not using the interactive shell. 您需要遍历光标并使用.forEach循环显式打印每个文档,因为您没有使用交互式shell。

db = db.getSiblingDB('importCSV');
db.people.find().forEach(function(doc) {
    printjson(doc);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM