Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗?更新问题,以使溢出。

3个月前关闭。



on-topic





我正在尝试使用拉操作从集合中的数组中删除元素,但是它不起作用
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代码
            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();
        }

我的这两个代码均无效。请帮帮我。谢谢

最佳答案

通过将photoList.photoId更改为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();
        }```

09-25 18:45