gpt4 book ai didi

java - 删除mongodb中的一条记录

转载 作者:行者123 更新时间:2023-12-02 00:12:13 26 4
gpt4 key购买 nike

我有一个这样的 Collection 。

      System
{
System_id: 100,

system_type:
{
[
{
Tenant_Id: 1,
Tenant_Info: "check",
Prop_Info: ...
},
{
Tenant_Id: 2,
Tenant_Info: "sucess",
Prop_Info: ...
} ]
}

我需要使用java api删除唯一一个tenant_id为1且system_id为100的嵌入文档。

我已尝试删除该文档。但整个文档都被删除了。我只需要删除tenant_id为1的嵌入文档。

            DBCollection collection=db.getCollection("system");
field.put("system_id",100);
field.put("system_type.Tenant_id", 1);
collection.remove(field);

请指导我如何删除它?我需要这样的输出。

            System
{
System_id: 100,

system_type:
{
[
{
Tenant_Id: 2,
Tenant_Info: "sucess",
Prop_Info: ...
} ]
}

最佳答案

由于您不删除整个文档,因此需要使用 $pull 调用 update运算符而不是删除:

BasicDBObject query = new BasicDBObject(
"System_Id", 100
);

BasicDBObject pull = new BasicDBObject(
"$pull", new BasicDBObject(
"system_type", new BasicDBObject(
"Tenant_Id", 1
)
)
);

collection.update(query, pull);

关于java - 删除mongodb中的一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12555947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com