我使用了来自Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is com.mongodb.util.JSONParseException:的代码,现在尝试调用
A
我得到的响应为空。
此外,下面的查询不会提取任何内容。
Person p = personRepository.findByAddresses_City("London NW1");
System.out.println("PERSON FOR ADDRESS = "+p);
这里:
db.getCollection('地址').find({})
Query query = new Query(Criteria.where("address.$id").is(2L));
List<Person> l = mongoTemplate.find(query, Person.class);
System.out.println(l);
和/* 1 */
{
"_id" : NumberLong(1),
"address" : "221b Baker Street",
"city" : "London NW1",
"state" : "London",
"zipcode" : NumberLong(12345),
"_class" : "com.example.demo.model.Address"
}
db.getCollection('person').find({})
如何解决此错误?
/* 1 */
{
"_id" : NumberLong(1),
"name" : "Achilles",
"age" : 0,
"addresses" : [],
"_class" : "com.example.demo.model.Person"
}
/* 2 */
{
"_id" : NumberLong(2),
"name" : "Hektor",
"age" : 0,
"addresses" : [
{
"$ref" : "address",
"$id" : NumberLong(1),
"$db" : "address"
}
],
"_class" : "com.example.demo.model.Person"
}
和
@Document(collection = "address")
public class Address {
@Id
private long addressId;
private String address;
private String city;
private String state;
private long zipcode;
public Address() {
System.out.println("CAlling default cons");
}
@PersistenceConstructor
public Address(long addressId, String address, String city, String state, long zipcode) {
this.addressId = addressId;
this.address = address;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}
// setter and getter... toString()..
}
答案 0 :(得分:0)
这按设计工作。 MongoDB不允许通过查询进行应用程序级联接,您需要对更复杂的查询使用聚合框架。因此,存储库查询仅允许按完整值(即Address
对象)或标识符查找DBRef。
如果将where子句固定为address.addressId
,第二个示例应该可以工作。
P.S .:请避免仅仅因为您在此处没有立即得到答案而提交机票。如果要提交票证,请确保将示例项目与测试用例一起附加。