【发布时间】:2021-05-27 15:46:10
【问题描述】:
我有这个模型:
const userSchema = new Schema({
email: {
type: String,
required: true,
unique: true
},
firstname: String,
lastname: String,
password: {
type: String,
required: true
},
activities: [{
description: {
type: String,
required: true
},
status: String,
from: Date,
to: Date
}]
}, { timestamps: true })
在获得User.findById() 的用户后,如何获得activities 数组中活动的ObjectId,以便执行(例如)delete 和update 操作?
编辑 1
我可以使用 console.log() 或在我的 GUI 中查看每个 ObjectId,因为即使我没有为每个元素指定字段 _id,MongoDB 也会为数组中的每个元素创建一个 ObjectId。这是一个示例:
USER ACTIVITIES [
{
_id: 603765c7bb1d1c24cc7f80ff,
description: 'Complete the project',
status: 'pending',
from: 2021-02-25T08:54:31.719Z,
to: 2021-02-25T08:54:31.719Z
},
{
_id: 60377672bb1d1c24cc7f8100,
description: 'Figure out how to get the ObjectId',
status: 'pending',
from: 2021-02-25T10:05:38.144Z,
to: 2021-02-25T10:05:38.144Z
}
]
我想访问这个_id,以便对单个活动执行操作
【问题讨论】:
标签: node.js arrays mongodb mongoose mongoose-schema