我有一個名爲「products」的集合,其中包含「bids」對象的數組。
我想找出每個產品的最高出價,爲此我在$ max與$ bids.bidamount字段上彙總產品。然而,這隻能給我最大的出價金額。如何投影最大聚合的所有出價字段。
這裏是一個示例文件
{
"_id" : ObjectId("58109a5138fe12215cfdc064"),
"product_id" : 2,
"item_name" : "Auction Item1",
"item_description" : "Test",
"seller_name" : "[email protected]",
"item_price" : "20",
"item_quantity" : 7,
"sale_type" : "Auction",
"posted_at" : "2016:10:26 04:58:09",
"expires_at" : "2016:10:30 04:58:09",
"bids" : [
{
"bid_id" : 1,
"bidder" : "[email protected]",
"bid_amount" : 300,
"bit_time" : "2016:10:26 22:36:29"
},
{
"bid_id" : 2,
"bidder" : "[email protected]",
"bid_amount" : 100,
"bit_time" : "2016:10:26 22:37:29"
}
],
"orders" : [
{
"buyer" : "[email protected]",
"quantity" : "2"
},
{
"buyer" : "[email protected]",
"quantity" : "3"
}
]
}
這裏是我的蒙戈查詢:
db.products.aggregate([
{
$project: {
bidMax: { $max: "$bids.bid_amount"}
}
}
])
其給出以下結果:
{
"_id" : ObjectId("58109a5138fe12215cfdc064"),
"bidMax" : 300
}
你嘗試加入投標領域該項目 ? – Veeram