在 mongodb 中运行 javascript 脚本(.js 文件),包括 js 中的另一个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22248730/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:37:25  来源:igfitidea点击:

Run javascript script (.js file) in mongodb including another file inside js

mongodb

提问by SexyMF

I want to write a long script for inserting and updating mongodb data.

我想写一个长脚本来插入和更新 mongodb 数据。

  1. Is it possible to call external js file that contains the script?
  2. Is it possible to include another js file from the running js file?
  1. 是否可以调用包含脚本的外部 js 文件?
  2. 是否可以从正在运行的 js 文件中包含另一个 js 文件?

回答by Sumeet

Use Load function

使用加载功能

load(filename)

You can directly call any .jsfile from the mongo shell, and mongo will execute the JavaScript.

您可以直接从 mongo shell调用任何.js文件,mongo 将执行 JavaScript。

Example: mongo localhost:27017/mydb myfile.js

示例mongo localhost:27017/mydb myfile.js

This executes the myfile.js script in mongo shell connecting to mydb database with port 27017 in localhost.

这将在 mongo shell 中执行 myfile.js 脚本,该脚本通过本地主机中的端口 27017 连接到 mydb 数据库。

For loading external js you can write

对于加载外部js,您可以编写

load("/data/db/scripts/myloadjs.js")

Suppose we have two js file myFileOne.js and myFileTwo.js

假设我们有两个 js 文件 myFileOne.js 和 myFileTwo.js

myFileOne.js

myFileOne.js

print('From file 1');
load('myFileTwo.js');     // Load other js file .

myFileTwo.js

myFileTwo.js

print('From file 2');

MongoShell

蒙戈壳牌

>mongo myFileOne.js

Output

输出

From file 1
From file 2

回答by Toumi

Yes you can. The default location for script files is data/db

是的你可以。脚本文件的默认位置是 data/db

If you put any script there you can call it as

如果你把任何脚本放在那里,你可以称之为

load("myjstest.js")      // or 
load("/data/db/myjstest.js")

回答by YeeHaw1234

Another way is to pass the file into mongo in your terminal prompt.

另一种方法是在终端提示中将文件传递给 mongo。

$ mongo < myjstest.js

This will start a mongo session, run the file, then exit. Not sure about calling a 2nd file from the 1st however. I haven't tried it.

这将启动一个 mongo 会话,运行文件,然后退出。但是不确定从第一个文件调用第二个文件。我没试过。

回答by raja ganapathy

for running mutilple js files

用于运行多个 js 文件

#!/bin/bash
cd /root/migrate/

ls -1 *.js | sed 's/.js$//' | while read name; do
     start=`date +%s`
     mongo localhost:27017/wbars $name.js;
     end=`date +%s`
     runtime1=$((end-start))
     runtime=$(printf '%dh:%dm:%ds\n' $(($runtime1/3600)) $(($secs%3600/60)) $(($secs%60)))
     echo @@@@@@@@@@@@@ $runtime $name.js completed @@@@@@@@@@@
     echo "$name.js completed"
     sync
     echo 1 > /proc/sys/vm/drop_caches
     echo 2 > /proc/sys/vm/drop_caches
     echo 3 > /proc/sys/vm/drop_caches
done

回答by ubèr

To call external file you can use :

要调用外部文件,您可以使用:

load ("path\file")

加载(“路径\文件”)

Exemple: if your file.js file is on your "Documents" file (on windows OS), you can type:

例如:如果您的 file.js 文件在您的“文档”文件中(在 Windows 操作系统上),您可以输入:

load ("C:\users\user_name\Documents\file.js")

加载 ("C:\users\user_name\Documents\file.js")