mongo-go-driver find a document by _id

前端 未结 2 1197
礼貌的吻别
礼貌的吻别 2021-01-05 09:29

I\'m trying to find a document by its auto generated _id field. Code below returns nothing:

var documentID bson.RawValue
documentID.Type = 7
documentID.Value         


        
2条回答
  •  隐瞒了意图╮
    2021-01-05 09:44

    You can use some thing like this:

    var userDB user
    objectIDS, _ := primitive.ObjectIDFromHex(userID)
    collectionUser := dBClient.Database("MyDatabase").Collection("Users")
    filter := bson.M{"_id": objectIDS}
    err := collectionUser.FindOne(ctx, filter).Decode(&userDB)
    if err != nil {
        fmt.Println("errror retrieving user userid : " + userID)
    }
    

提交回复
热议问题