MongoTemplate在mongo shell和spring mongoTemplate中的Pull查询不起作用。

huangapple go评论97阅读模式
英文:

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&#39;m trying to remove an element from an array in a collection using pull operation but it&#39;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(&quot;photoList&quot;, new BasicDBObject(&quot;photoList.photoId&quot;,photoId));
            mongoTemplate.updateFirst(new Query(Criteria.where(&quot;projectId&quot;).is(projectId)), update, &quot;spyne_share&quot;);
        } 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(&quot;projectId&quot;).is(projectId));
			Update update = new Update().pull(&quot;photoList&quot;, new BasicDBObject(&quot;photoId&quot;, photoId));
			mongoTemplate.updateMulti(query, update, SpyneShareProject.class);
		} catch (Exception e) {
			e.printStackTrace();
		}```

</details>



huangapple
  • 本文由 发表于 2020年3月17日 02:26:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/60711342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定