【发布时间】:2017-04-15 10:04:20
【问题描述】:
我正在尝试学习如何使用 mongo 查询深入数据树。具体来说,我正在尝试删除{"object": 'to remove'}下方的对象
{
"_id" : ObjectId("7840f22736341b09154f7ebf"),
"username" : "nmay",
"fname" : "Nate",
"lname" : "May",
"data" : [
{
"monthNum" : 1,
"year" : 2016,
"days" : [
{
"date" : "2016-01-01T06:00:00.000Z",
"type1" : [],
"type2" : []
},
{
"date" : "2016-01-02T06:00:00.000Z",
"type1" : [
{"object": 'to remove'}
],
"type2" : []
}
]
}
]
}
到目前为止,我知道如何查询用户 _id,但我不确定如何使用每个数组中的索引删除所需的对象。在这个例子中我想删除data[0].days[1].type1[0]
这是我目前的查询:
app.delete('/user/:id/data/:monthIndex/days/:dayIndex/type1/:type1Index', function (req, res, next) {
var monthIndex = parseInt(req.params.monthIndex); // these console the value properly
var dayIndex = parseInt(req.params.dayIndex); // -1 is applied to the parameter to translate to array position
var type1Index = parseInt(req.params.type1Index);
db.users.update(
{ _id: mongojs.ObjectId(req.params.id) },
{ $pull: data.monthIndex.days.dayIndex.type1.type1Index }
);
}
它给了我错误
ReferenceError: 数据未定义
有人可以演示如何将此查询传递给我的索引参数以删除所需的对象吗?
【问题讨论】:
标签: mongodb express typescript