(1) Atualizar (puxar)
reply
elemento de matriz: Este código atualizará o documento; que remove o elemento específico (subdocumento) do
reply
variedade:// Query criteria for topic and reply
String topicId = "5e5e4d4bb431502946c15342";
String topicReplyId = "07a0293a-22a1-45fb-9aa2-775fa24e9915";
MongoOperations mongoTemplate = new MongoTemplate(MongoClients.create(), "test");
Query query = Query.query(Criteria
.where("topic._topicId").is(topicId)
.and("topic.reply._replyId").is(topicReplyId));
Update update = new Update().pull("topic.$.reply", new Document("_replyId", topicReplyId));
mongoTemplate.updateFirst(query, update, "topics"); // "topics" is the collection name
[ EDITAR ADICIONAR ]
(2) Consulta de agregação para obter a
reply
documento: db.topics.aggregate( [
{ $unwind: "$topic" },
{ $match: { "topic._topicId": topicId } },
{ $unwind: "$topic.reply" },
{ $match: { "topic.reply._replyId": topicReplyId } },
{ $project: { _id: 0, reply: "$topic.reply" } }
] ).pretty()
Isso retorna:
{
"reply" : {
"_replyId" : "07a0293a-22a1-45fb-9aa2-775fa24e9915",
"username" : "test1",
"content" : "reply1",
"date" : 1583240955561
}
}