mongodb arrayFilters java 使用方式

我!!!永远!!!为!!!爆栈!!!打call!!!

当初使用mongo时,没用过多考虑更新的问题,采取了双层嵌套格式来存储数据,现在需求变动,要有更新,百度了一天,没有解决,爆哭,尝试在爆栈搜索解决方法,又又又又一次,爆栈救了我的命!!!

我的文档格式

{
    "_id" : "1",
    "personCerts" : [ 
        {
            "perId" : "546",
            "perName" : "老坛酸菜",
            "certifications" : [ 
                {
                    "certId" : "111",
                    "regType" : "aaa",
                    "type" : "二级"
                }
            ]
        }, 
        {
            "perId" : "334",
            "perName" : "朱浩",
            "certifications" : [ 
                {
                    "certId" : "01438668",
                    "regType" : "wwww",
                    "type" : "二级"
                }, 
                {
                    "certId" : "222",
                    "regType" : "ttt",
                    "type" : "二级"
                }
            ]
        }, 
        {
            "perId" : "788",
            "perName" : "牛大龙",
            "certifications" : [ 
                {
                    "certId" : "01365043",
                    "regType" : "rrr",
                    "type" : "二级"
                }
            ]
        }, 
        {
            "perId" : "677",
            "perName" : "高恒",
            "certifications" : [ 
                {
                    "certId" : "沪231191921594",
                    "regType" : "RY_ZCLB_072",
                    "type" : "二级"
                }
            ]
        }, 
        {
            "perId" : "45667",
            "perName" : "袁慧",
            "certifications" : [ 
                {
                    "certId" : "00640150",
                    "regType" : "uuu",
                    "type" : "二级"
                }
            ]
        }
    ],
    "createDate" : ISODate("2020-05-17T09:21:25.790Z"),
    "lastUpdate" : ISODate("2020-06-23T05:59:45.624Z")
},{
    "_id" : "2",
    "personCerts" : [ 
        {
            "perId" : "1",
            "perName" : "kk",
            "certifications" : [ 
                {
                    "certId" : "5443",
                    "regType" : "aaa",
                    "type" : "二级"
                }
            ]
        }
    ],
    "createDate" : ISODate("2020-05-17T09:21:25.790Z"),
    "lastUpdate" : ISODate("2020-06-23T05:59:45.624Z")
}

更新需求为 :将 _id = 1 ,perId = 334 , certId = 222 的 regType 更新为 ooo

爆栈的写法:

https://stackoverflow.com/questions/51725518/mongodb-java-spring-update-nested-object?r=SearchResults&s=2|40.2023

模仿后,我的写法

Bson filters = Filters.and(Filters.eq("_id","1"),
                                Filters.elemMatch(
                                        "personCerts",
                                        Filters.and(
                                                Filters.eq("perId","334"),
                                                Filters.elemMatch("certifications",Filters.eq("certId","222"))
                                        )
                                )
                        );

Bson update = Updates.set("personCerts.$.certifications.$[certification].regType","OOO");
        
Bson filter = Filters.eq("certification.certId", "222");
        
UpdateOptions updateOptions = new UpdateOptions().arrayFilters(Collections.singletonList(filter));

MongoCollection<Document> collection = mongoTemplate.getDb().getCollection("col");

documents.updateOne(filters,update,updateOptions);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章