英文:
MongoTemplate Pull query not working in mongo shell and spring mongoTemplate
问题
我试图使用 pull 操作从集合中的数组中删除一个元素,但它不起作用。
*JSON*
"previousCoverSize": 0,
"photoList": [
{
"photoId": "shrU17266w",
"origUrl": "vendors/photographer/1503983007582/projects/3585/photos/shrU17266w.jpeg",
"webUrl": "vendors/photographer/1503983007582/projects/3585/photos/shrU17266w.jpeg",
"mobileUrl": "vendors/photographer/1503983007582/projects/3585/photos/shrU17266w.jpeg",
"fileType": "2B6A6418.jpeg",
"dateAdded": ISODate("2020-02-11T13:10:13.041+05:30"),
"name": "2B6A6418.jpeg",
"category": "Birthday shoot",
"vendorId": "1503983007582",
"vendorType": "photographer",
"isPrivate": true,
"selectedByClient": false,
"setOnHomePage": false,
"clientFav": false,
"isWebBanner": true,
"isMobileBanner": false,
"isThumbnail": false,
"isFolder": false,
"exifData": {
"Make": "Canon",
"Model": "Canon EOS 5D Mark III",
"FocalLength": "85",
"Flash": "Flash did not fire, compulsory flash mode",
"DateTimeOriginal": "2020:02:03 19:34:02",
"GPSLongitude": "Canon EOS 5D Mark III",
"Size": "99823",
"Software": "Adobe Photoshop Lightroom Classic 8.0 (Windows)"
},
"altText": "Ridhaan Birthday project 2B6A6418.jpeg image.",
"rejected": false,
"indexNumber": 2
}
*Mongo shell 代码*
db.spyne_share.update({"projectId":"3585"},{$pull:{"photoList":{$elemMatch:{photoId:"shrU17266w"}}}})
*Java 代码*
```java
try {
Update update = new Update()
.pull("photoList", new BasicDBObject("photoList.photoId", photoId));
mongoTemplate.updateFirst(new Query(Criteria.where("projectId").is(projectId)), update, "spyne_share");
} catch (Exception e) {
e.printStackTrace();
}
这两段代码都不起作用。请帮我解决。谢谢
<details>
<summary>英文:</summary>
# I'm trying to remove an element from an array in a collection using pull operation but it's not working #
*JSON*
"previousCoverSize" : 0,
"photoList" : [
{
"photoId" : "shrU17266w",
"origUrl" : "vendors/photographer/1503983007582/projects/3585/photos/shrU17266w.jpeg",
"webUrl" : "vendors/photographer/1503983007582/projects/3585/photos/shrU17266w.jpeg",
"mobileUrl" : "vendors/photographer/1503983007582/projects/3585/photos/shrU17266w.jpeg",
"fileType" : "2B6A6418.jpeg",
"dateAdded" : ISODate("2020-02-11T13:10:13.041+05:30"),
"name" : "2B6A6418.jpeg",
"category" : "Birthday shoot",
"vendorId" : "1503983007582",
"vendorType" : "photographer",
"isPrivate" : true,
"selectedByClient" : false,
"setOnHomePage" : false,
"clientFav" : false,
"isWebBanner" : true,
"isMobileBanner" : false,
"isThumbnail" : false,
"isFolder" : false,
"exifData" : {
"Make" : "Canon",
"Model" : "Canon EOS 5D Mark III",
"FocalLength" : "85",
"Flash" : "Flash did not fire, compulsory flash mode",
"DateTimeOriginal" : "2020:02:03 19:34:02",
"GPSLongitude" : "Canon EOS 5D Mark III",
"Size" : "99823",
"Software" : "Adobe Photoshop Lightroom Classic 8.0 (Windows)"
},
"altText" : "Ridhaan Birthday project 2B6A6418.jpeg image.",
"rejected" : false,
"indexNumber" : 2
}
*Mongo shell code*
db.spyne_share.update({"projectId":"3585"},{$pull:{"photoList":{$elemMatch:{photoId:"shrU17266w"}}}})
*Java Code*
```try {
Update update = new Update()
.pull("photoList", new BasicDBObject("photoList.photoId",photoId));
mongoTemplate.updateFirst(new Query(Criteria.where("projectId").is(projectId)), update, "spyne_share");
} catch (Exception e) {
e.printStackTrace();
}
Both these codes of mine are not working. Please help me out. Thanks
答案1
得分: 0
将photoList.photoId更改为photoId后,获得了完美的响应
try {
Query query = Query.query(Criteria.where("projectId").is(projectId));
Update update = new Update().pull("photoList", new BasicDBObject("photoId", photoId));
mongoTemplate.updateMulti(query, update, SpyneShareProject.class);
} catch (Exception e) {
e.printStackTrace();
}
英文:
Got perfect response by changing photoList.photoId to photoId
Query query = Query.query(Criteria.where("projectId").is(projectId));
Update update = new Update().pull("photoList", new BasicDBObject("photoId", photoId));
mongoTemplate.updateMulti(query, update, SpyneShareProject.class);
} catch (Exception e) {
e.printStackTrace();
}```
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论