我想创建一个内部具有唯一名称的架构,该架构可用于在不同集合中创建条目。
当前架构代码:
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const folderSchema = mongoose.Schema({
name: { type: String, required: true, unique: true }
})
folderSchema.plugin(uniqueValidator);
module.exports = mongoose.model('SubFolders', folderSchema);
当前代码:
router.post('/sub', (req, res, next) => {
// unique not working
const subFolder = new subFolderModel({
name: req.body.subFolderName
});
mongoose.connection.collection(req.body.rootFolderName).insertOne(subFolder);
});
我可以创建两个具有相同名称的条目