如何在SpringBoot中从MongoDB中仅读取特定的JSON节点

huangapple go评论66阅读模式
英文:

How to read only a particular JSON node from MongoDb in SpringBoot

问题

我正在尝试通过我的Springboot项目从MongoDb中读取单个JSON节点。

以下是在Mongo中保存的示例JSON:

    "a": {
        "b": {
            "c": {
                "d": "value1",
                "e": "value2"
            }
        }
    }

我尝试了以下代码:

   public String get() {
       BasicDBObject query = BasicDBObject.parse("{\"a.b.c.d\": \"value1\"}");
       FindIterable<Document> dumps = mongoOperations.getCollection("data").find(query);
       return dumps.first().toJson();
   }

我得到的响应如下:

    {
        "_id": {
            "$oid": "5e8aa42602ef9f05bf35ff59"
        },
        "a": {
            "b": {
                "c": {
                    "d": "value1",
                    "e": "value2"
                }
            }
        }
    }

但是我只需要节点 a.b.c.d,我知道在读取整个JSON后可以在Java中处理它。但我的目的是仅读取所需部分。

期望的输出为:

    {
       "a": {
            "b": {
                "c": {
                    "d": "value1"
                }
            }
        }
    }

另请注意,我将保存具有随机结构的JSON,因此没有与之关联的POJO。如果有人可以为我提供指导,那将非常有帮助。

英文:

I'm trying to read a single JSON node from MongoDb via my Springboot project.

here is the sample JSON saved in Mongo:

    &quot;a&quot;: {
            &quot;b&quot;: {
                &quot;c&quot;: {
                    &quot;d&quot;: &quot;value1&quot;,
                    &quot;e&quot;: &quot;value2&quot;
                }
            }
        }

I tried:


   public String get() {
	   BasicDBObject query = BasicDBObject.parse(&quot;{\&quot;a.b.c.d\&quot;: \&quot;value1\&quot;}&quot;);
	   FindIterable&lt;Document&gt; dumps = mongoOperations.getCollection(&quot;data&quot;).find(query);
	   return dumps.first().toJson();
   }

response I got:


    {
        &quot;_id&quot;: {
            &quot;$oid&quot;: &quot;5e8aa42602ef9f05bf35ff59&quot;
        },
        &quot;a&quot;: {
            &quot;b&quot;: {
                &quot;c&quot;: {
                    &quot;d&quot;: &quot;value1&quot;,
                    &quot;e&quot;: &quot;value2&quot;
                }
            }
        }
    }

but I only need the node a.b.c.d, I know I can do it in java after reading the whole JSON. But my intention is to read only the needed part.

Expected Output:


    {
       &quot;a&quot;: {
            &quot;b&quot;: {
                &quot;c&quot;: {
                    &quot;d&quot;: &quot;value1&quot;
                }
            }
        }
    }

Also please note that I'll be saving JSONs with random schema so no POJOs associated with it. It would be really helpful if somebody can guide me on this.

答案1

得分: 1

你可以使用 find 查询中的 _projection_ 来仅获取指定的字段。在尝试了 MongoDB Spring Data 2.2.6 之后,可以这样做:

MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "testDB");

Query qry = new Query();
qry.fields().include("a.b.c.d").exclude("_id");
List<Document> list = mongoOps.find(qry, Document.class, "collection");
list.forEach(doc -> System.out.println(doc.toJson()));

输出结果:

{"a": {"b": {"c": {"d": "value1"}}}}
英文:

You can get the specified field only, using a projection with the find query. Tried with MongoDB Spring Data 2.2.6:

MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), &quot;testDB&quot;);

Query qry = new Query();
qry.fields().include(&quot;a.b.c.d&quot;).exclude(&quot;_id&quot;);
List&lt;Document&gt; list = mongoOps.find(qry, Document.class, &quot;collection&quot;);
list.forEach(doc -&gt; System.out.println(doc.toJson()));

The output:

{&quot;a&quot;: {&quot;b&quot;: {&quot;c&quot;: {&quot;d&quot;: &quot;value1&quot;}}}}

huangapple
  • 本文由 发表于 2020年4月6日 12:52:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61053161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定