如何在mongodb Java中的对象列表中仅过滤匹配的对象
> db.reports.find({'productViews.productId' : {$lte: "1234"}},{"productViews" : 1})
返回
{ "_id" : ObjectId("5c062a2dfcd262039c7cb592"),
"productViews" :
[
{ "productId" : "1234", "views" : "2" },
{ "productId" : "3214", "views" : "34" }
]
}
上面的查询返回数组的所有值,但是我只想要与提供的对象匹配的对象。请建议如何使用Java
fun getProductViews(mongoDatabase: MongoDatabase) {
val report = mongoDatabase.getCollection("reports")
val result = report.find(Filters.lte("productViews.productId", "20"))
print(result)
}
@Document(collection = "reports")
data class Report(
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
var reportId: String = Date().toString(),
var productViews: ArrayList<ProductViews>,
var categoryViews: ArrayList<CategoryViews>,
)