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")
}
]
}