Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mongodb存储库中编写自定义查询_Mongodb_Spring Boot_Mongorepository - Fatal编程技术网

在mongodb存储库中编写自定义查询

在mongodb存储库中编写自定义查询,mongodb,spring-boot,mongorepository,Mongodb,Spring Boot,Mongorepository,我正在mongo存储库中编写自定义查询,如下所示: public interface AuthenticationTokenRepository extends MongoRepository<AuthenticationToken, String> { AuthenticationToken save(AuthenticationToken token); AuthenticationToken findByToken(String token);

我正在mongo存储库中编写自定义查询,如下所示:

public interface AuthenticationTokenRepository extends MongoRepository<AuthenticationToken, String> 
{

    AuthenticationToken save(AuthenticationToken token);

    AuthenticationToken findByToken(String token);

    @Query("{'user.username' : ?0}")
    AuthenticationToken findByUserId(String username);
}
我在身份验证表中的数据如下:

> db.authenticationToken.find().pretty()
{
    "_id" : ObjectId("5565b4d444ae3b1a9228be87"),
    "_class" : "com.samepinch.domain.user.AuthenticationToken",
    "token" : "545c1d10-d769-41cf-a47c-2d698ec4df72",
    "user" : {
        "_id" : ObjectId("5565b4d444ae3b1a9228be86"),
        "age" : 0,
        "username" : "dinesh.dhiman@oodlestechnologies.com",
        "firstName" : "Dinesh Dhiman",
        "email" : "dinesh.dhiman@oodlestechnologies.com",
        "gender" : "male",
        "createdDate" : ISODate("2015-05-27T12:13:08.562Z"),
        "updatedDate" : ISODate("2015-05-27T12:13:08.562Z")
    },
    "createdDate" : ISODate("2015-05-27T12:13:08.605Z"),
    "updatedDate" : ISODate("2015-05-27T12:13:26.436Z")
}

我想根据userId获取数据。有人能帮我吗。

我也试过同样的例子。试试这个示例代码。这对我有用

@Query("{user._id:?0}")
AuthenticationToken findByUser(String userId);

在Spring的某些版本中,必须使用ObjectId类而不是String<所以你可以试试这样的东西

@Query("{'user._id' : ?0}")
    AuthenticationToken findByUserId(ObjectId objectId);
String userId = "someUserId";
repository.findByUserId(new ObjectId(userId));
如果您想在代码中使用此查询,它将如下所示

@Query("{'user._id' : ?0}")
    AuthenticationToken findByUserId(ObjectId objectId);
String userId = "someUserId";
repository.findByUserId(new ObjectId(userId));

文档中没有userId字段