????????最近在將之前python+mongodb的項目用java重構(gòu),所以不可避免的涉及到j(luò)ava操作mongodb,這里采用的數(shù)據(jù)庫框架是spring-data-mongodb,至于為什么采用這個框架,可能類似于JPA的形式直接打動了我。在實際的使用中,確實沒有python方便,其中遇到了一些問題,這里記下來,以便自己回憶,也希望能幫助大家。
? ? 數(shù)據(jù)格式:

單層操作 ? :修改值1,order_name
Query query=new Query(Criteria.where("_id").is("ObjectId("5cda615557f78256fa4c6741")").and("answers.order_number").is(1));
Update update=new Update();
update.set("answers.$.order_name","修改的值1!??!");
//個人理解:$相當(dāng)于占位符,可以在查詢更新時,及時的傳值
mongoTemplate.updateFirst(query,update,"answer:表名");
雙層操作:修改值2,hit_keywords
Query query=new Query(Criteria.where("_id").is("5cda615557f78256fa4c6741"));
Update update=new Update();
update.set("answers.$[first].answer_list.$[second].hit_keywords","修改的值2!??!");
// ? 個人理解:$[]可以用來指定位置,并通過 ?filterArray過濾器進行過濾
update.filterArray("first.order_number",1);
update.filterArray("second.q_number",2);
mongoTemplate.updateFirst(query,update,"answer:表名");
多層指令:可以通過$[]設(shè)置多個占位符修改
注意:$[]只在mongodb3.6版本及以上才能使用,否則會報錯filterArray指令不存在;