【发布时间】:2014-10-10 01:29:10
【问题描述】:
我需要问一个关于文档中嵌套对象状态的问题,如下所示:
{
"_id" : ObjectId("5437248f2dfbc82fcafa9733"),
"_class" : "conference.Speaker",
"speakerId" : NumberLong(0),
"name" : "John Doe",
"talks" : [{
"talkId" : NumberLong(0),
"when" : ISODate("2014-10-17T15:00:00Z"),
"title" : "Stuff"
}]
}
这个文档看起来不错,它是由 Speaker 和 Talk 两个类生成的,它们都没有任何注释,而前者有一个 List of the laters 作为属性。
我需要得到一个具有特定名称的谈话列表。 这是我想做的事情:
BasicQuery query = new BasicQuery("{'talks.title' : 'Stuff'}");
org.springframework.data.mapping.model.MappingException: Invalid path reference talks.title! Associations can only be pointed to directly or via their id property! 失败
我猜它不能使用 Id 属性(因为 Talk 对象没有它自己的 id),而且我不知道“可以直接指向”是什么意思。
我的查询和/或映射有什么问题?
【问题讨论】:
-
我猜这是因为 talks 是一个数组。直接指出,应该是
talks.index.title(index = 0, 1, ...)——当然这种风格出乎你的意料。另一种方法是:Query query = new Query().addCriteria(Criteria.where("talks.title").is("Stuff"));。不知道是不是你想要的。
标签: mongodb spring-data spring-data-mongodb