執行聚合后,我有以下資料:
{
"_id" : ObjectId("61ea385114e22e000c58a502"),
"stu_id" : "h224566378",
"stu_inf" : [
{
"_id" : ObjectId("61ea3a35c34a334514e7e8df"),
"stu_id" : "h224566378",
"gender" : "male",
"class" : "A",
}
]
}
如果我希望資料如下:
{
"_id" : ObjectId("61ea385114e22e000c58a502"),
"stu_id" : "h224566378",
"stu_class" : "A"
}
如何使用“聚合”來滿足我的需求?
我希望提取的“stu_inf”中的“類值”成為新欄位的值。
我嘗試以下方式,它可以得到結果:
db.collection_name.aggregete([
...
{"$project": {"stu_id": 1, "stu_class": "$stu_inf.class"}},
{"$unwind": "$stu_class"}
])
但我想知道是否還有其他方法?
uj5u.com熱心網友回復:
采用$first
db.collection.aggregate([
{
$project: {
"stu_id": 1,
"stu_class": {
$first: "$stu_inf.class"
}
}
}
])
mongoplayground
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/465221.html
標籤:mongodb
