const Koa = require(‘koa‘)
const app = new Koa()
const Router = require(‘koa-router‘)
let router = new Router()
const mongoose = require(‘mongoose‘)
const fs = require(‘fs‘)
router.get(‘/insertAllGoodsInfo‘, async (ctx) => {
fs.readFile(‘./newGoods.json‘, ‘utf8‘, (err, data) => {
data = JSON.parse(data)
let saveCount = 0
const Goods = mongoose.model(‘Goods‘)
data.map((value, index) => {
console.log(value)
//放入建模,建模的js在下面
let newGoods = new Goods(value)
newGoods.save().then(() => {
saveCount++
console.log(‘成功‘ + saveCount)
}).catch(error => {
console.log(‘失败:‘ + error)
})
})
})
ctx.body = "开始导入数据"
})
module.exports = router;
2.建模的js
const mongoose = require(‘mongoose‘) //引入Mongoose
const Schema = mongoose.Schema //声明Schema
let ObjectId = Schema.Types.ObjectId //声明Object类型
//创建我们的用户Schema
const goodsSchema = new Schema({
ID: { unique: true, type: String },
GOODS_SERIAL_NUMBER: String,
SHOP_ID: String,
SUB_ID: String,
GOOD_TYPE: Number,
STATE: Number,
NAME: String,
ORI_PRICE: Number,
PRESENT_PRICE: Number,
AMOUNT: Number,
DETAIL: String,
BRIEF: String,
SALES_COUNT: Number,
IMAGE1: String,
IMAGE2: String,
IMAGE3: String,
IMAGE4: String,
IMAGE5: String,
ORIGIN_PLACE: String,
GOOD_SCENT: String,
CREATE_TIME: String,
UPDATE_TIME: String,
IS_RECOMMEND: Number,
PICTURE_COMPERSS_PATH: String
}, {
collections: ‘Goods‘
})
//发布模型
mongoose.model(‘Goods‘, goodsSchema)
3.发布到index.js里面
let goods = require(‘./appApi/goods.js‘)
router.use(‘/goods‘,goods.routes())