【问题标题】:How to query MongoDb documents using the indices of embedded arrays如何使用嵌入式数组的索引查询 MongoDb 文档
【发布时间】: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


    【解决方案1】:

    不幸的是,在 MongoDB 中,无法通过单个操作通过数字索引删除数组元素。为此,您需要先取消设置所需的元素,然后删除生成的 null-valued 字段。

    您的代码应如下所示:

    db.users.update(
        { _id : mongojs.ObjectId(req.params.id) },
        { $unset : { 'data.0.days.1.type1.0' : 1 } }
    );
    db.users.update(
        { _id : mongojs.ObjectId(req.params.id) },
        { $pull : { 'data.0.days.1.type1' : null } }
    );
    

    @bob 编辑:要传入你必须构建查询字符串的参数,这很难看:

    var unset = {};
    unset['$unset'] = {};
    unset.$unset['data.' + req.params.monthIndex + '.days.' + req.params.dayIndex + '.foods.' + req.params.foodIndex] = 1;
    
    db.users.update( { _id : mongojs.ObjectId(req.params.id) }, unset );
    
    var pull = {};
    pull['$pull'] = {};
    pull.$pull['data.' + req.params.monthIndex + '.days.' + req.params.dayIndex + '.foods'] = null;
    
    db.users.update( { _id : mongojs.ObjectId(req.params.id) }, pull );
    

    【讨论】:

    • 这适用于硬编码的索引号,但我怎样才能传入一个数字?
    • @NateMay 我从来没有使用过 Express/Typescript,所以我真的不知道用这种语言连接字符串和变量的正确语法是什么。在纯 JavaScript 中,这看起来像 'data.' + monthIndex + '.days.' + dayIndex + '.type1.' + type1Index,也许这也适合你。
    • 动态构建查询。此外update 已被弃用。使用updateOneupdateMany
    • @NateMay 我几乎保留了您的编辑,但请注意,当您从数组中提取一个元素时,您实际上 想要指示任何索引,因为查询会自行解决。如果你想删除多个元素,你仍然只需要一个 pull 操作,因为它会删除所有计算结果为 null 的元素。
    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 2021-03-24
    • 2015-02-06
    • 2021-04-14
    • 2013-10-16
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    相关资源
    最近更新 更多