【发布时间】:2021-04-15 21:57:04
【问题描述】:
我正在使用 Express 和 Mongoose 执行一些数据库操作。当尝试填充键包含空格的路径时,它基本上会被忽略。
MongoDB 模型:
const OrgCrimeSchema = new Schema({
gracePeriod: { type: Date, default: Date.now() },
Technical: {
description: String,
difficulty: Number,
owner: { type: Schema.Types.ObjectId, ref: 'User' },
},
'Social Engineering': { // This one causes issues
description: String,
difficulty: Number,
owner: { type: Schema.Types.ObjectId, ref: 'User' },
},
});
查找 + 填充:
const now = Date.now();
const allOrgCrimes = await OrgCrime.find({ gracePeriod: { $lte: now } })
.populate('Technical.owner', 'name')
.populate('Social Engineering.owner', 'name');
console.log(allOrgCrimes['Social Engineering'][0].owner);
//5fca3b4a86e77b5c8e58b683
console.log(allOrgCrimes['Technical'][0].owner);
// { name: 'npc_alice', _id: 5fae6d7ee60018434108369c }
我假设路径没有被填充,因为键中有一个空格。我尝试了点符号和输入{path: 'Social Engineering', select: 'name -id'},但没有运气。
有什么办法可以不用重写架构结构吗?
【问题讨论】:
标签: mongodb express mongoose schema populate