本文介绍了无法使用 MongoTemplate 从嵌套数组中提取并查询返回子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using mongodb in springboot. And here is a part of my data:

{
  "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 中的一个对象),我尝试以下查询:

(1) I try to pull a reply(a object of in java) from a topic, I try these queries:

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)部分来遍历元素

然后我决定使用第三种方式:

Then I decide to use the third way:

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

我尝试新建一个 ReplyEntity 回复实体,但我遇到了第二个问题:

I try to new a ReplyEntity replyEntity, and I got my second problem:

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

(2) How can I get the subdocument from a document?

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.我只想要回复,怎么做?非常感谢.

I used the query but I get the outer-document(topic), include two reply on the example above of topic1.I just want the reply,how can make it? Thanks a lot.

推荐答案

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

(1) Update (pull) reply array element:

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

This code will update the document; that is removes the specific element (sub-document) from the reply array:

// 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文档:

(2) Aggregation query to get the reply document:

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

返回:

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

这篇关于无法使用 MongoTemplate 从嵌套数组中提取并查询返回子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:01