Mongodb删除子文档

时间:2014-12-20 10:50:42

标签: javascript mongodb spring-data-mongodb

我正在尝试删除mongoDb中的子文档,但无法成功。我尝试过$ update,$ push和$ pull但是我没有成功。

我有以下文件:

db.users.findOne({"_id": ObjectId("545677a9e4b0e0ef9379993c")})
{
    "_class" : "com.xxx.xxx.server.da.User",
    "_id" : ObjectId("545677a9e4b0e0ef9379993c"),
    "bookSummaries" : [
        {
            "_id" : ObjectId("545677e7e4b0e0ef9379993d"),
            "isbn" : "2746969644"
            "title": "test title"
        },      
        {
            "_id" : ObjectId("546a522a44ae4f5e652fdca7"),           
            "loanSummaries" : [
                {
                    "loanStatus" : "REQUESTED",
                    "loanId" : "5473992044aec466a619290c"
                },
                {
                    "loanStatus" : "REQUESTED",
                    "loanId" : "5473997d44aec466a619290d"
                },
                {
                    "loanStatus" : "REQUESTED",
                    "loanId" : "547605a744ae0f0d558ae757"
                }
            ],
            "testArray" : [
                {
                    "title" : "Back to the future",
                    "id" : "test"
                },
                {
                    "title" : "Back to the future",
                    "id" : "test2"
                },
                {
                    "title" : "Back to the future",
                    "id" : "test3"
                },
                "test ",
                "test ",
                "test 2",
                "test 2",
                {
                    "loanStatus" : "REQUESTED"
                }
            ],
            "title" : "Back to the future"
        }
    ]
}

我正在尝试创建查询:

  1. 仅删除此特定子文档的整个“testArray”子文档
  2. 删除特定loanSummaries为此特定子文档提供其loanId
  3. 你能帮我创建这些查询吗?我见过这样的几个帖子,但问题不一样。

    非常感谢

3 个答案:

答案 0 :(得分:2)

我最近遇到了同样的问题,我必须删除嵌套的子阵列。在查询中,您需要标识要删除的确切对象,然后在更新查询中,您可以使用$符号作为要拉出的数组中对象的索引。

var query = {
       "_id" : ObjectId(someUserId),
       "bookSummaries._id" : ObjectId(bookId), //might also work with isbn as long as it is uniq
       "bookSummaries.loanSummaries.loanId" : loanId
 };

var updateQuery = {
      "$pull" : {
          "bookSummaries.$.loanSummaries" : {
                 "loanId": loadId
          }
      }
}

更新

我在我的shell中尝试了以下查询,但运行正常。您必须输入适当的ID。

var query = { "_id" : ObjectId("53b73b1108fe927c0e00007f"),  "bookSummaries._id" : ObjectId("53b73b1108fe927c0e00007f"), "bookSummaries.loanSummaries.loanId" : "53b73b1108fe927c0e00007f" }

var updateQuery = { "$pull" : { "bookSummaries.$.loanSummaries" : {  "loanId": "53b73b1108fe927c0e00007f"  } } }

更新2

如果您已经知道要删除的项目/对象的索引,则可以使用以下查询来实现此目的。

找到您要编辑的文件

var query = {
   "_id" : ObjectId(someUserId),
};

取消设置要删除的对象

var update1 = {
  "$unset" : {
      "bookSummaries.1.loanSummaries.2" : 1
  }
}

拉出值为null的对象。

var update2 = {
  "$pull" : {
      "bookSummaries.1.loanSummaries" : null
  }
}

要删除你基本上可以使用相同的查询。

找到您要编辑的文件

var query = {
   "_id" : ObjectId(someUserId),
};

取消设置要删除的对象

var update1 = {
  "$unset" : {
      "bookSummaries.0.testArray" : 1
  }
} 

答案 1 :(得分:0)

由于MongoDB提供了完整的JavaScript环境,因此您可以轻松地操作文档并将数据保存回数据库。

*注意:如果你使用php或python等MongoDB,有相同的方法可以做到这一点。

1.仅为此特定子文档删除整个“testArray”子文档,例如index(1)

db.users.find({"_id": ObjectId("545677a9e4b0e0ef9379993c")}).forEach(function (user) {
    delete user.bookSummaries[1].testArray;
    //Saveing the changes 
    db.users.save(user);
 });
  1. 删除特定loanSummaries为此特定子文档提供其loanId,例如id 5473992044aec466a619290c

    db.users.find({"_id": ObjectId("545677a9e4b0e0ef9379993c")}).forEach(function (user) {
        var loanToDelete = "5473992044aec466a619290c";
        var loanIndexToDelete = -1;
    
        var i;
        for(i in user.bookSummaries[1].loanSummaries) {
            if(user.bookSummaries[1].loanSummaries[i].loanId === loanToDelete) {
                loanIndexToDelete = i;
                break;
            }
        }
    
        if (loanIndexToDelete > -1) {
          delete user.bookSummaries[1].loanSummaries[loanIndexToDelete];
        }
    
        //Saving the changes
        db.users.save(users);
    });
    

答案 2 :(得分:0)

我必须做类似的事情,但是删除多个子文档而不是一个。我的查询包含一个ID数组,这对我有用:

User.updateOne(
      { _id: userId },
      {
        $pull: { bookSummaries: { _id: { $in: ids } } },
      });