var Mock = require(‘mockjs’);
const mongoose = require(“mongoose”);
// module.exports = {
// students: Mock.mock({
// ‘students|50’: [{
// name: “@cname()”,
// age: ‘@integer(18,30)’,
// gender: ‘@string(“男女”,1)’,
// iphone: /1[3][0-9]{9}$/,
// address: ‘@county(true)’,
// email: ‘@email()’,
// zip: ‘@zip()’
// }]
// })
// }
const studentSchema = new mongoose.Schema({
name: String,
age: Number,
gender: String,
iphone: String,
address: String,
email: String,
zip: String,
})
const studentsModel = mongoose.model(“students”, studentSchema, “students”);
const { students } = Mock.mock({
“students|40”: [
{
name: “@cname()”,
age: “@integer(20, 30)”,
gender: “@string(‘男女’, 1)”,
iphone: /^1[0-9]{10}$/,
address: “@county(true)”,
email: “@email()”,
zip: “@zip()”
}
]
});
mongoose.connect(
“mongodb://localhost:27017/F70”,
{
useNewUrlParser: true,
useUnifiedTopology: true
},
async () => {
// 3、操作数据
const data = await studentsModel.create(students);
console.log(data);
}
);
-
1 ↩︎