我正在建立一个数据库,并且在测试过程中,使用伪数据时,ObjectId数组未保存在数据库中
我使用内置的mongoose函数进行了检查,当我检查它是否为object-ids时,它返回true
模式是这样的
const projectSchema = new mongoose.Schema({
name: String,
description: String,
tasks: [{ type: Schema.Types.ObjectId, ref: "Task" }],
feedBack: String
});
let Project = mongoose.model("Project", projectSchema);
const taskSchema = new mongoose.Schema({
name: String
});
let Task = mongoose.model("Task", taskSchema);
我正在添加这样的收藏
async function dbLoad() {
let taskArr = projectDummy.map(project => project.tasks);
let taskCollection = [].concat(...taskArr).map(task => {
let t = new Task();
t.name = task.name;
return t;
});
await Task.insertMany(taskCollection);
let projectCollection = [];
for (let project of projectDummy) {
let taskArr = [];
for (let task of project.tasks) {
let data = await Task.findOne({ name: task.name });
taskArr.push(data["_id"]);
}
projectCollection.push(
new Project({
name: project.name,
description: project.description,
tasks: taskArr
})
);
}
await Project.insertMany(projectCollection);
}