我有posts集合和嵌套的文档注释。简单地说,文档模式如下所示:
{
"_id" : ObjectId("5867e5e64f768509c59f9392"),
"userId" : 1,
"comments": [
{
"id" : 1,
"text": "a"
},
{
"id" : 2,
"text": "b"
}
]
}我只想投射所要求的评论。我找到了一个可以接受的解决方案。(也欢迎只返回注释对象的解决方案)
> db.posts.find({userId: 1, 'comments.id': 1}, {'comments.$': 1});
{ "_id" : ObjectId("5867e5e64f768509c59f9392"), "comments" : [ { "id" : 1, "text" : "a" } ] }然后,我尝试将其应用到mongodb存储库接口中,并使用查询注释。
@Query(
value = ""
+ "{"
+ " 'userId' : '?1',"
+ " 'comments.id': '?2',"
+ "}",
fields = "'comments.$'"
)
Note findComment(String userId, String commentId);但我被投了个例外。是否有更好的方法来处理?
发布于 2016-12-31 18:35:07
您的@Query格式不佳。
尝尝这个。
@Query( value="{ 'userId' : ?0 , 'comments.id': ?1 }", fields = "{ 'comments.$' : 1}")https://stackoverflow.com/questions/41410382
复制相似问题