猿问

提问如何在Golang中将mongodb的命令转换为Bson

这是收藏列表


{

    _id : autoIncrement

    "P_NAME" : "Name",

    "P_LIST" : [

        {

            _id : autoIncrement

            "P_TYPE" : "HELL",

            "P_POOL" : "Not Use"

        }

    ]

}

我在 MongoDB 中使用它时使用了这个命令。


db.P.find({},{"P_LIST": {$elemMatch: {_id:2}}, _id: 0})

同样在 Golang 中,我尝试搜索这样的条件,但没有成功。


collection.Find(context.TODO(), bson.M{bson.M{}, bson.M{"P_LIST":bson.M{"$elemMatch":bson.M{"_id":2}}, bson.M{"_id": 0}}})

Golang 如何将 Find 命令与 MongoDB 等条件和过滤器一起使用?


莫回无
浏览 229回答 1
1回答

呼啦一阵风

你Find打错了,你将过滤器和投影传递给 Find 的过滤器参数。func (coll *Collection) Find(ctx context.Context, filter interface{},    opts ...*options.FindOptions) (*Cursor, error)尝试这样做:collection.Find(    context.TODO(),    nil,    options.Find().SetProjection(bson.M{        "P_LIST": bson.M{            "$elemMatch": bson.M{"_id": 2},            "_id":        0,        },    }))在此处查看更多详细信息Find
随时随地看视频慕课网APP

相关分类

Go
我要回答