How to filter documents based on an embedded array?

后端 未结 1 1368
我寻月下人不归
我寻月下人不归 2021-01-14 21:22

After reviewing this page, specifically this query

db.scores.find(
   { results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)

I used the follo

相关标签:
1条回答
  • 2021-01-14 22:00

    You need to use $elemMatch(projection). Something like below should work.

    import static com.mongodb.client.model.Projections.elemMatch;
    
    Bson filters = and(eq("userId", userId));
    Bson projections = fields(elemMatch(ARRAY_FIELD_NAME, eq("id", id)), excludeId());
    
    0 讨论(0)
提交回复
热议问题