无法使用 MongoTemplate 从嵌套数组中提取并查询 return 子文档

Not able to pull from nested array and query return sub-document using MongoTemplate

我在springboot中使用mongodb。这是我的一部分数据:

{
  "topic": [
    {
      "_topicId": "5e5e4d4bb431502946c15342",
      "name": "testName0",
      "username": "test0",
      "date": 1583238474961,
      "reply": [
        {
          "_replyId": "38d29dcb-1a79-4788-b721-5fbe700cc99d",
          "username": "test0",
          "content": "reply0",
          "date": 1583240780072
        },
        {
          "_replyId": "07a0293a-22a1-45fb-9aa2-775fa24e9915",
          "username": "test1",
          "content": "reply1",
          "date": 1583240955561
        }
      ]
    },
    {
      "_topicId": "5e5e4d4bb431502946c15343",
      "name": "testName1",
      "username": "test1",
      "date": 1583238475241,
      "reply": []
    }
  ]
}

我有两个问题:

(1) 我尝试从 topic 中提取一个 reply(java 中的一个对象),我尝试这些查询:

Query query = Query.query(Criteria.where("_topicId").is(topicId));
Update update = new Update().pull("reply.$._replyId", topicReplyId);
mongoTemplate.updateFirst(query, update, "colletionName");

我得到一个错误位置运算符没有找到查询所需的匹配项

Query query = Query.query(Criteria.where("_topicId").is(topicId));
Update update = new Update().pull("reply._replyId", topicReplyId);
mongoTemplate.updateFirst(query, update, "colletionName");

然后我得到一个错误无法使用(reply._replyId)的部分(_replyId)来遍历元素

那我决定用第三种方式:

Query query = Query.query(Criteria.where("_topicId").is(topicId));
Update update = new Update().pull("reply", replyEntity);
mongoTemplate.updateFirst(query, update, "colletionName");

我尝试新建一个 ReplyEntity replyEntity,我遇到了 第二个问题

(2)如何从文档中获取子文档?

Query query = Query.query(Criteria.where("_topicId").is(topicId).and("reply._replyId").is(replyId));
TopicEntity t = mongoTemplate.findOne(query, TopicEntity.class, "colletionName");

我使用了查询,但我得到了外部文档(topic),在上面的 topic1 示例中包含两个 reply。 我只想要reply,怎么弄呢?非常感谢。

(1)更新(拉取)reply数组元素:

此代码将更新文档;即从 reply 数组中删除特定元素(子文档):

// 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



[编辑添加]

(2)聚合查询获取reply文档:

db.topics.aggregate( [
  { $unwind: "$topic" },
  { $match: { "topic._topicId": topicId } },
  { $unwind: "$topic.reply" },
  { $match: { "topic.reply._replyId": topicReplyId } },
  { $project: { _id: 0, reply: "$topic.reply" } }
] ).pretty()

这个returns:

{
        "reply" : {
                "_replyId" : "07a0293a-22a1-45fb-9aa2-775fa24e9915",
                "username" : "test1",
                "content" : "reply1",
                "date" : 1583240955561
        }
}