How to find collections by its nested object's objectId in Spring Data using repository interface?

蓝咒 提交于 2019-12-23 07:03:02

问题


I have a collection in MongoDB that has items like this one:

{
    "_id" : ObjectId("53e4d31d1f6b66e5163962e3c"),
    "name" : "bob",
    "nestedObject" : {
        "_id" : ObjectId("53f5a623cb5e4c1ed4f6ce67")
        //more fields...
    }
}

Java representation of this item looks following:

public class SomeObject {
    @Id
    private String id;
    private String name;
    private NestedObject nestedObject;

    //getters and setters
}

The Repository interface is defined like this:

public interface SomeObjectRepository extends MongoRepository<SomeObject, String> {
    public List<SomeObject> findByName(String name);
    public List<SomeObject> findByNestedObjectId(String id);
    //some other find functions
}

Now, findByName(String name) is working as it should be, but findByNestedObjectId(String id) returns nothing.

Question is: is it possible to find collection items by it's nested object's attribute using repository interface? If not, what is the recommended way to approach this problem? Is it possible without reimplementing whole repository?


回答1:


Spring-data-mongodb would not convert _id field to ObjectId type automatically in nested class on query operation. You should convert it manually. For example:

public List<SomeObject> findByNestedObjectId(String id) {
    Query query = Query.query(new Criteria("nestedObject._id", convertToObjectId(id)));
    return mongoTemplate.find(query, SomeObject.class);
}

Object convertToObjectId(Object id) {
    if (id instanceof String && ObjectId.isValid(id)) {
        return new ObjectId(id);
    }
    return id;
}



回答2:


I've figured out how to solve this.

Change the parameter type to org.bson.types.ObjectId; from String

public List<SomeObject> findByNestedObjectId(ObjectId id);

and when you call it use

 repositoryName.findByNestedObjectId(new ObjectId(theIdString));



回答3:


If NestedObject looks like this:

class NestedObject {

  @Id String id;
}

and the String value you hand into the query is a valid ObjectId the query:

findByNestdObjectId(String id);

should work. If it doesn't feel free to create a ticket in out JIRA and provide a tiny test case to reproduce it.




回答4:


You can specify the mongoDB query used by the repository with @Query. I'm not 100% the below query is excact but something along this:

public interface SomeObjectRepository extends MongoRepository<SomeObject, String> {
    public List<SomeObject> findByName(String name);
    @Query("{nestedObject._id : ?0}")
    public List<SomeObject> findByNestedObjectId(String id);
    //some other find functions
}



回答5:


This worked for me on 1.6.2 spring-data-mongodb. But having issues with latest versions.

public interface SomeObjectRepository extends MongoRepository<SomeObject, String> {
    public List<SomeObject> findByName(String name);
    @Query("nestedObjectProperty._id : { $oid : ?0}")
    public List<SomeObject> findByNestedObjectId(String id);
    //some other find functions
}



回答6:


I'm using the following for Spring-boot; works like charm for me:

@Query("nestedObjectProperty.$id : ?0")


来源:https://stackoverflow.com/questions/25441514/how-to-find-collections-by-its-nested-objects-objectid-in-spring-data-using-rep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!