[英]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.