【发布时间】:2017-11-28 13:26:39
【问题描述】:
我有这两个“链接”的 POJO 实体:
public class User {
public ObjectId id; // this is mapped to "_id" in the MongoDB
public String userName;
...
}
public class Purchase {
public ObjectId id; // this is mapped to "_id" in the MongoDB
public ObjectId userId;
public transient User user;
public String productTitle;
...
}
我们的想法是只保留用于购买的用户 ID,并使用 $lookup 聚合函数按需加载(或:加入)适当的用户文档。因为 Purchase.user 属性不应该保存在 MongoDB 中,所以它被声明为瞬态。这能行吗?
现在,在我的 PurchaseRepository 中,我正在尝试像这样实现它:
public void getSinglePurchaseWithUser(Bson filter, SingleResultCallback<Purchase> callback) {
Document match = new Document("$match", filter);
Document lookupFields = new Document("from", "Users");
lookupFields.put("localField", "userId");
lookupFields.put("foreignField", "_id");
lookupFields.put("as", "user");
Document lookup = new Document("$lookup", lookupFields);
List<Document> pipeline = Arrays.asList(match, lookup);
AggregateIterable<Purchase> output = this.collection.aggregate(pipeline);
output.first(callback);
}
很遗憾,purchase.user 在结果中始终为空。我还尝试手动投影来明确地读取用户:
Document projectFields = new Document("_id", 1);
projectFields.put("userId", 1);
projectFields.put("user", "$user");
...
Document project = new Document("$project", projectFields);
List<Document> pipeline = Arrays.asList(match, lookup, project);
但这会引发错误读取:
org.bson.codecs.configuration.CodecConfigurationException:一个 使用 AutomaticPojoCodec 解码时发生异常。 解码为“购买”失败,但出现以下异常:
解码“用户”失败。 readStartDocument 只能在以下情况下调用 CurrentBSONType 是 DOCUMENT,而不是当 CurrentBSONType 是 ARRAY 时。
自定义编解码器或 PojoCodec 可能需要显式配置和 注册来处理这种类型。
我错过了什么?
【问题讨论】:
标签: java mongodb mongodb-query