这是我从mongodb-go-driver获得的功能:
func MongodbFindOne(key, value string) bson.M {
var result bson.M
opts := options.FindOne().SetShowRecordID(false)
_ = Collection.FindOne(context.TODO(), bson.M{key: value}, opts).Decode(&result)
return result
}
该功能效果很好,但我得到_id
了结果。我知道mongodb查询要从查询结果中排除一个字段,但是我不知道如何与FindOne()
功能一起使用它:
db.removeIdDemo.find({},{_ id:0});
db.collection.find({},{_ id:0,t_number:1})。toArray()。map(function(ele){return ele.t_number});
从mongo结果中删除_id(nodejs):
app.get('/itesms', function(req, res) { items.find({}, { _id: 0 }).toArray(function (err, array) { res.send(array); }) });
要从结果中排除字段,请使用投影。使用FindOneOptions.SetProjection()
设置的投影。
要专门排除该_id
字段:
err = c.FindOne(ctx,
bson.M{key: value},
options.FindOne().SetProjection(bson.M{"_id": 0}),
).Decode(&result)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句