models:

    1. const mongoose = require("mongoose");
    2. //1.连接本地数据库
    3. var mongodb = mongoose.createConnection( 'mongodb://127.0.0.1:27017/movies', {useNewUrlParser: true});
    4. var Schema = mongoose.Schema;
    5. module.exports = {
    6. mongodb,
    7. Schema
    8. };
    1. const {mongodb,Schema} = require("./baseDB/db-movies");
    2. //2.在本地定义一个Schema和远程数据库的字段映射
    3. var MovieSchema =new Schema({
    4. pic:String,
    5. title:String,
    6. raiting:String,
    7. slogo:String,
    8. evaluate:String,
    9. labels:Array
    10. });
    11. // 3.创建数据模型,操作数据库 table是需要访问的集合名
    12. function MovieModel(table){
    13. return mongodb.model(table,MovieSchema,table); //是我们获取的数据库里叫table(变量)的那张表
    14. }
    15. module.exports = MovieModel;

    在路由中使用:

    1. const router = require("koa-router")();
    2. const MovieModel = require("../../models/movie");
    3. router.get("/api/movie",async ctx=>{
    4. let {m} = ctx.query;
    5. var data = await MovieModel(m).find().sort({rating:-1});
    6. ctx.body = {
    7. code:200,
    8. res:data,
    9. request:"GET /api/top250"
    10. }
    11. })
    12. module.exports = router;