我们如何通过Java驱动程序查询嵌入式文档? [英] How can we we give query for embedded documents through java driver?

查看:79
本文介绍了我们如何通过Java驱动程序查询嵌入式文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过mongodb中的java查询访问嵌入式文档.从一个简单的文档中很容易,但是我们如何从嵌入式文档中访问呢?

i want to access the embedded document through java query from mongodb.from a simple document its easy but how can we access from embedded document ?

推荐答案

如果我对您的理解正确,那么您可以在下面找到问题的答案.

If I understand you correctly, you can find the answer of your question below.

假设您有以下嵌套文档.

Say you have the following nested document.

{ "key1" : "value1",
  "key2" : {
             "key21" : "value21",
             "key22" : "value22"
           }
}

如果您要查询嵌套文档,则可以使用以下Java代码访问嵌入的对象.

If you mean to make query on the nested documents then you can access the embedded object using the following java code.

DBCollection coll = db.getCollection("collectionName");
BasicDBObject query = new BasicDBObject();
query.put("key2.key21", new BasicDBObject("$eq", "value21"));
DBCursor cur = coll.find(query);

如果您打算从文档中提取嵌入式文档,则可以使用 Map/Reduce 聚合框架.

If you mean to extract embedded document from the document then you can use Map/Reduce or Aggregation framework.

Ian Daniel仍然添加了用于插入您所请求的嵌套文档的代码.您也可以访问页面以查看一些详细示例.

Ian Daniel is still added the code to insert nested documents that you asked. You can also visit this page to see some detailed examples.

这篇关于我们如何通过Java驱动程序查询嵌入式文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆