springdata-mongodb中Criteria.Where("XXX").elemMatch(Criteria.where("AAA").is(aaa))和Criteria.Where("XXX.AAA").is(aaa)的不同

1. query.addCriteria(Criteria.where("_id").is(id).and("isDelete").is(0).and("comments").elemMatch(Criteria.where("commentId").is(commentId).and("userId").is(userId)));
她的底层是sql代码是:

db.getCollection("flow_data").find({"_id": ObjectId("61a03858991ba42fe067d8c3"), "isDelete": NumberInt("0"), "comments": {"$elemMatch": {"commentId": "4687d5ac0a5b47f3aad9db281beff12d", "userId": NumberInt("100063")}}})
2. query.addCriteria(Criteria.where("_id").is(id).and("isDelete").is(0).and("comments.commentId").is(commentId).and("comments.userId").is(userId));
她的底层是sql代码是:
db.getCollection("flow_data").find({"_id": ObjectId("61a03858991ba42fe067d8c3"), "comments.commentId": "4687d5ac0a5b47f3aad9db281beff12d", "comments.userId": NumberInt("100063"), "isDelete": NumberInt("0")}

踩坑:一开始是使用的第一种方法去更新红色的那条数,却发现更新了蓝色那条,然后换成第二中方法,就能精准的更新了
{ "_id": ObjectId("61a03858991ba42fe067d8c3"), "flowNo": "1000631637890136", 中间省略 "isDelete": NumberInt("0"), "comments": [ { "commentId": "c6a0d1b310c0482ea0a0d5658a1c6551", "userId": NumberInt("100063"), "content": "哈哈", "fileUrls": "", "time": NumberLong("1637890559"), "isRevocation": NumberInt("1"), "rvctnTime": NumberLong("1637891868") }, { "commentId": "4687d5ac0a5b47f3aad9db281beff12d", "userId": NumberInt("100063"), "content": "哈哈哈哈哈", "fileUrls": "", "time": NumberLong("1637891940"), "isRevocation": NumberInt("0") }, { "commentId": "de63c0fb4c5841d38b1a97fa33ec942f", "userId": NumberInt("100063"), "content": "哈哈哈哈哈", "fileUrls": "", "time": NumberLong("1637891974"), "isRevocation": NumberInt("0") }, { "commentId": "8d55e71a7b5148bfa8657f4fb3ae16b4", "userId": NumberInt("100063"), "content": "哈哈哈哈", "fileUrls": "[{\"fileName\":\"滴滴出行行程报销单-3.pdf\",\"filePath\":\"http://42.192.81.91:8089/u/63/100063/202111/ef79f22987a649378934eaa4c1193662.pdf\",\"fileExt\":\".pdf\",\"fileId\":\"滴滴出行行程报销单-3.pdf0\"}]", "time": NumberLong("1637894157"), "isRevocation": NumberInt("0") } ] }
posted @ 2021-11-26 15:39  菜菜菜鸟秦  阅读(1866)  评论(0)    收藏  举报