spring + mongodb
请教个问题,我现在想操作3层的数字不知道怎么操作。例如:
{"one":[{"two":[{"three":""}]}]} 现在我想删除{"three":""}这个,但不知道怎么操作,unset pull 都试了,不对,不知道是不是我写错还是怎样,求解答?
spring mongodb 对多层数组的操作问题?求指教
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
你知我知皆知 2024-08-03 22:05关注以下回答参考 皆我百晓生、券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。
你想要在MongoDB中删除一个特定的文档,你需要使用
deleteOne()方法或者findOneAndDelete()方法。这两个方法都可以用来删除一个匹配条件的文档。如果你想要删除的是一个数组中的第一个元素,你可以直接使用
remove()方法来删除它。但是请注意,这只会删除数组的第一个元素,并不会影响到其他元素。Document document = new Document(); document.put("one", new Document()); document.get("one").put("two", new Document()); document.get("one").get("two").put("three", ""); MongoCollection<Document> collection = mongoTemplate.getCollection(collectionName); collection.deleteOne(document);如果数组中有多个元素,那么你需要使用
findOneAndRemove()方法,这个方法会根据指定的查询条件返回第一个匹配的文档,并且删除它。Document document = new Document(); document.put("one", new Document()); document.get("one").put("two", new Document()); document.get("one").get("two").put("three", ""); MongoCollection<Document> collection = mongoTemplate.getCollection(collectionName); Document matchingDoc = collection.findOneAndRemove(new Document().append("one", new Document().append("two", new Document()).append("three", "")));以上就是如何在Spring和MongoDB中删除一个包含多个层次的对象的方法。希望对你有所帮助!
解决 无用评论 打赏 举报