【发布时间】:2021-08-14 22:52:58
【问题描述】:
数据:
{
"_id": ObjectId("54bb201aa3a0f26f885be2a3"),
"photo": "test",
"likeCount": 2,
"likes": [11, 10]
}
Mongo 查询
db.photos.find(
{
"_id": ObjectId("54bb201aa3a0f26f885be2a3"),
},
{
"photo": 1
"likeCount": 1,
"likes": {
"$elemMatch": { "$eq": 11 }
}
}
)
来自查询的响应:-
{
"_id": ObjectId("54bb201aa3a0f26f885be2a3"),
"photo": "test",
"likeCount": 2,
"likes": [11]
}
我附加的 Spring boot Mongo 模板查询:-
query.fields().elemMatch("likes", new Criteria("likes").is(11));
这会导致 likes=Document{{$elemMatch=Document{{likes=11}}}} 所以喜欢在响应中不会为空
【问题讨论】:
-
你也在使用 MongoRepository 吗?
-
是的,我也在使用 MongoRepository
-
那你为什么不使用
findByLikesIn(List<Integer> likesList) -
我正在使用投影和 elemMatch 获取所有数据,我的过滤器是“_id”不喜欢
-
您的文档有很多字段吗?我认为您误解了 jpa 查询方法。我说找到点赞能有11个的文档
标签: mongodb spring-boot mongodb-query mongotemplate