mongo shell中可以直接执行js文件,这在一次操作大量数据时非常有用,可以提前编写好js文件。本文会先说明执行js的时机,然后通过一个插入数据的脚本演示如何执行js。文中使用的脚本文件可以从百度云下载:mongodb_query_language
js执行时机
主要有2种方式:
- 连接mongo服务时,将js文件名放在命令行末尾,类似
$ mongo localhost:27017/test myjsfile.js
- 连接mongo服务后,通过load方法加载执行js,类似
load("myjstest.js")
更多参考Write Scripts for the mongo Shell
连接mongodb数据库
在进行下面的操作前需要先使用mongo shell连接mongodb数据库,参考启动mongod并使用mongo shell连接。
插入数据
这个名为loadMovieDetailsDataset.js
脚本的部分内容如下:
db = db.getSiblingDB("video");
db.movieDetails.drop();
db.movieDetails.insertMany([
{"title":"Once Upon a Time in the West","year":1968,"rated":"PG-13","runtime":175,"countries":["Italy","USA","Spain"],"genres":["Western"],"director":"Sergio Leone","writers":["Sergio Donati","Sergio Leone","Dario Argento","Bernardo Bertolucci","Sergio Leone"],"actors":["Claudia Cardinale","Henry Fonda","Jason Robards","Charles Bronson"],"plot":"Epic story of a mysterious stranger with a harmonica who joins forces with a notorious desperado to protect a beautiful widow from a ruthless assassin working for the railroad.","poster":"http://ia.media-imdb.com/images/M/MV5BMTEyODQzNDkzNjVeQTJeQWpwZ15BbWU4MDgyODk1NDEx._V1_SX300.jpg","imdb":{"id":"tt0064116","rating":8.6,"votes":201283},"tomato":{"meter":98,"image":"certified","rating":9,"reviews":54,"fresh":53,"consensus":"A landmark Sergio Leone spaghetti western masterpiece featuring a classic Morricone score.","userMeter":95,"userRating":4.3,"userReviews":64006},"metacritic":80,"awards":{"wins":4,"nominations":5,"text":"4 wins \u0026 5 nominations."},"type":"movie"},
......
]);
在js脚本中无法使用use video
命令切换数据库,只能使用db.getSiblingDB("video")
,更多参考db.getSiblingDB()。db.movieDetails.drop();
用于彻底删除数据库video中的movieDetails这个collection,更多关于drop命令参考db.collection.drop()
执行插入的命令为:
load("/Users/chenxin/Downloads/mongodb_query_language/loadMovieDetailsDataset/loadMovieDetailsDataset.js")
终端输出true
,表示执行成功。可以使用db.movieDetails.find({})
查看是否插入数据:
{ "_id" : ObjectId("5b4469e5f08b7fb5d97364f0"), "title" : "Once Upon a Time in the West", "year" : 1968, "rated" : "PG-13", "runtime" : 175, "countries" : [ "Italy", "USA", "Spain" ], "genres" : [ "Western" ], "director" : "Sergio Leone", "writers" : [ "Sergio Donati", "Sergio Leone", "Dario Argento", "Bernardo Bertolucci", "Sergio Leone" ], "actors" : [ "Claudia Cardinale", "Henry Fonda", "Jason Robards", "Charles Bronson" ], "plot" : "Epic story of a mysterious stranger with a harmonica who joins forces with a notorious desperado to protect a beautiful widow from a ruthless assassin working for the railroad.", "poster" : "http://ia.media-imdb.com/images/M/MV5BMTEyODQzNDkzNjVeQTJeQWpwZ15BbWU4MDgyODk1NDEx._V1_SX300.jpg", "imdb" : { "id" : "tt0064116", "rating" : 8.6, "votes" : 201283 }, "tomato" : { "meter" : 98, "image" : "certified", "rating" : 9, "reviews" : 54, "fresh" : 53, "consensus" : "A landmark Sergio Leone spaghetti western masterpiece featuring a classic Morricone score.", "userMeter" : 95, "userRating" : 4.3, "userReviews" : 64006 }, "metacritic" : 80, "awards" : { "wins" : 4, "nominations" : 5, "text" : "4 wins & 5 nominations." }, "type" : "movie" }
.......
{ "_id" : ObjectId("5b4469e5f08b7fb5d9736503"), "title" : "Star Trek II: The Wrath of Khan", "year" : 1982, "rated" : "PG", "runtime" : 113, "countries" : [ "USA" ], "genres" : [ "Action", "Adventure", "Drama" ], "director" : "Nicholas Meyer", "writers" : [ "Gene Roddenberry", "Harve Bennett", "Jack B. Sowards", "Jack B. Sowards" ], "actors" : [ "William Shatner", "Leonard Nimoy", "DeForest Kelley", "James Doohan" ], "plot" : "With the assistance of the Enterprise crew, Admiral Kirk must stop an old nemesis, Khan Noonien Singh, from using the life-generating Genesis Device as the ultimate weapon.", "poster" : "http://ia.media-imdb.com/images/M/MV5BMTcwNjc5NjA4N15BMl5BanBnXkFtZTcwNDk5MzI4OA@@._V1_SX300.jpg", "imdb" : { "id" : "tt0084726", "rating" : 7.7, "votes" : 86687 }, "tomato" : { "meter" : 88, "image" : "certified", "rating" : 8, "reviews" : 49, "fresh" : 43, "consensus" : "Considered by many fans to be the best of the Star Trek movies, Khan features a strong plot, increased tension, and a sharp supporting performance from Ricardo Montalban.", "userMeter" : 90, "userRating" : 3.8, "userReviews" : 84279 }, "metacritic" : 71, "awards" : { "wins" : 2, "nominations" : 9, "text" : "2 wins & 9 nominations." }, "type" : "movie" }
Type "it" for more
实际展示了20条数据,这里有省略。可以执行it
命令查看下面20条数据。
网友评论