【发布时间】:2026-01-10 17:35:01
【问题描述】:
此代码在 go mgo 库中运行良好 结果界面{}
err = getCollection.Find(bson.M{}).Select(bson.M{"_id": 1}).One(&result)
但我想使用 go mongo-driver 库执行此操作 我试过下面的代码,但它不像上面的那样工作
err = getCollection.FindOne(ctx, bson.M{}, options.FindOne().SetProjection(bson.M{"_id": 1})).Decode(&result)
我的test收集数据是
example{
"_id":ObjectId(),
"Name":"qwert"
}
有人建议我如何在 mongo-driver 中实现这一点?
【问题讨论】:
-
您对投影的使用是正确的。它会在结果中返回名称字段吗?
result的类型是什么?在调用此函数之前,您确定名称字段为空吗?