【发布时间】:2021-04-28 00:07:37
【问题描述】:
我有一个如下所示的项目架构:
const ProjectSchema = new mongoose.Schema({
name: {
type: String,
Required: true,
trim: true
},
description: {
type: String,
},
devices: [{
name: {type: String, Required: true},
number: {type: String, trim: true},
deck: {type: String},
room: {type: String},
frame: {type: String}
}],
cables: {
type: Array
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
adminsID: {
type: Array
},
createdAt: {
type: Date,
default: Date.now
}
我想从“设备”数组中查询一个对象。 我能够添加、删除和显示该数组中的所有子文档,但我发现很难在数组中获得与 _id 条件匹配的单个对象。
我得到的最接近的是这个(我要求:'/:id/:deviceID/edit' 其中“:id”是 Project ObjectId。
let device = await Project.find("devices._id": req.params.deviceID).lean()
console.log(device)
它为我提供了以下信息:
[
{
_id: 6009cfb3728ec23034187d3b,
cables: [],
adminsID: [],
name: 'Test project',
description: 'Test project description',
user: 5fff69af08fc5e47a0ce7944,
devices: [ [Object], [Object] ],
createdAt: 2021-01-21T19:02:11.352Z,
__v: 0
}
]
我知道这可能真的是微不足道的问题,但我已经测试了不同的解决方案,但似乎没有什么对我有用。感谢理解
【问题讨论】: