简体   繁体   English

mongodb Java驱动程序。 如何通过引用返回对象

[英]mongodb java driver. How to return object by reference

For example, i have two collections "animals" and "food". 例如,我有两个集合“动物”和“食物”。 In collection "animals" there is a field food, which is reference to collection "food" (not embedded). 在“动物”收藏中有一种野外食品,是对“食物”收藏(未嵌入)的引用。 Im using mongo java driver: 我正在使用mongo java驱动程序:

    DBCollection collection = db.getCollection("animals");
    DBObject fields = new BasicDBObject("name", 1);
    fields.put("food", 1);
    fields.put("_id", 0);
    DBObject project = new BasicDBObject("$project", fields );
    collection.aggregate(project);

it return name and Id of object "food". 它返回对象“食物”的名称和ID。 So what would be the proper way to get fields of object "food"? 那么,获取对象“食物”字段的正确方法是什么?

The short answer is that you can't do that sort of thing automatically in MongoDB. 简短的答案是,您无法在MongoDB中自动执行此类操作。 It does not have the concept of a 'join'. 它没有“联接”的概念。 You'll just have to make a separate query. 您只需要进行一个单独的查询。

This section of the documentation on Database Reference should help clarify. 有关数据库参考文档的这一部分将有助于阐明。

To resolve DBRefs, your application must perform additional queries to return the referenced documents. 若要解析DBRef,您的应用程序必须执行其他查询以返回引用的文档。 Many drivers have helper methods that form the query for the DBRef automatically. 许多驱动程序都有帮助程序方法,这些方法会自动形成DBRef的查询。 The drivers [1] do not automatically resolve DBRefs into documents. 驱动程序[1]不会自动将DBRef解析为文档。 DBRefs provide a common format and type to represent relationships among documents. DBRef提供了一种通用的格式和类型来表示文档之间的关系。 The DBRef format also provides common semantics for representing links between documents if your database must interact with multiple frameworks and tools. 如果数据库必须与多个框架和工具进行交互,则DBRef格式还提供了表示文档之间链接的通用语义。

Oftentimes, its best to store things as embedded documents, especially if you would have to 'manually join' those documents often. 通常,最好将内容存储为嵌入式文档,特别是如果您必须经常“手动加入”这些文档时。 Just depends on your data and how you need to query it. 仅取决于您的数据以及查询方式。 YMMV 因人而异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用MongoDB Java驱动程序返回正确的数据类型 - How to return the correct datatype with MongoDB Java Driver 从mongodb对象 - Java驱动程序返回属性 - Return attributes from mongodb object- Java driver MongoDB Java驱动程序-对象类型 - MongoDB Java driver - Object types 如何使用Java驱动程序访问MongoDB中嵌套在数组中的对象 - How to access object nested inside an array in MongoDB using Java driver 如何使用 MongoDB Java 驱动程序更新数组中的 object? - How to update an object in a array using MongoDB Java Driver? java jdbc:sqlite没有合适的驱动程序。 我认为类路径问题 - java jdbc:sqlite no suitable driver. classpath issues i think 如何在Java中将对象的引用作为参数返回 - How to return reference to object as parameter in Java 如何在 Java 中使用 Selenium 自动化测试登录 Gmail? 现在他们阻止登录测试浏览器驱动程序。 有办法吗? - How to Sign-in Gmail using Selenium automation testing in Java? Now that they block sign in from testing browser driver. Is there a way? PostgreSQL JDBC驱动程序的.jar文件。 如何配置? - .jar file for postgreSQL JDBC driver. How to configure? MongoDB Java驱动程序:如何从Java驱动程序插入任何集合 - MongoDB Java Driver: How to insert into any collection from Java Driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM