开发者社区> 问答> 正文

MongoTemplate Pull查询在mongo shell和springtemple中不起作用

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

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

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 09:30:13 1172 0
1 条回答
写回答
取消 提交回答
  • 通过将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();
            }```
    

    回答来源:Stack Overflow

    2020-03-22 09:30:44
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Shell 脚本速查手册 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载