在mongodb中运行javascript脚本(.js文件),包括js内的另一个文件 [英] Run javascript script (.js file) in mongodb including another file inside js
本文介绍了在mongodb中运行javascript脚本(.js文件),包括js内的另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写一个长脚本来插入和更新mongodb数据.
I want to write a long script for inserting and updating mongodb data.
- 是否可以调用包含脚本的外部js文件?
- 是否可以从正在运行的js文件中包含另一个js文件?
推荐答案
Use Load function
load(filename)
您可以直接从mongo shell中调用任何 .js 文件,然后mongo将执行JavaScript.
You can directly call any .js file from the mongo shell, and mongo will execute the JavaScript.
示例:mongo localhost:27017/mydb myfile.js
这将在mongo shell中执行myfile.js脚本,该脚本通过本地主机中的端口27017连接到mydb数据库.
This executes the myfile.js script in mongo shell connecting to mydb database with port 27017 in localhost.
要加载外部js,您可以编写
For loading external js you can write
load("/data/db/scripts/myloadjs.js")
假设我们有两个js文件myFileOne.js和myFileTwo.js
Suppose we have two js file myFileOne.js and myFileTwo.js
myFileOne.js
print('From file 1');
load('myFileTwo.js'); // Load other js file .
myFileTwo.js
print('From file 2');
MongoShell
>mongo myFileOne.js
输出
From file 1
From file 2
这篇关于在mongodb中运行javascript脚本(.js文件),包括js内的另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文