findOneAndUpdate(updateQuery,updateSet,returnFields)的返回类型是什么,以及如何获取返回的值?

I want to update some fields in DB and also want to it to return some fields, can you suggest how to retrieve the return fields?

so i am using here,

returnFields := map[string]interface{}{"order_id":1} 

data := FindAndUpdateVerticalsOffers(updateQuery, updateFields, returnFields)

How to get order_id from "data":


func FindAndUpdateVerticalsOffers(updateQuery map[string]interface{}, updateFields interface{}, returnFields map[string]interface{}) map[string]interface{} {

    session := db.GetSession()
    defer session.Close()
    collection := session.DB("").C(VerticalsOffersName)
    updateSet := bson.M{"$set": updateFields}
    return collection.FindOneAndUpdate(updateQuery, updateSet, returnFields)
}

I want to update some fields in DB and also want to it to return some fields,

If you're using mongo-go-driver (currently v1.1), you can utilise FindOneAndUpdate() which finds a single document and updates it, returning either the original or the updated.

The method accepts argument for FindOneAndUpdateOptions, which supports projection. For example:

collection := client.Database("dbName").Collection("collName")

// Sets projection (or return fields)
findUpdateOptions := options.FindOneAndUpdateOptions{}
findUpdateOptions.SetProjection(bson.M{"order_id": 1})

result := collection.FindOneAndUpdate(context.TODO(), 
                                      bson.M{"foo":1}, 
                                      bson.M{"$set": bson.M{"bar":1}}, 
                                      &findUpdateOptions)

doc := bson.M{}
err = result.Decode(&doc)

The above query will match a document where field foo is 1, update field bar to 1, and return only order_id as the result. Note that by default the _id field is also returned. You can suppress the _id field from being projected by setting it to 0.

Please note that the return type of FindOneAndUpdate is a SingleResult object, which represents a single document returned from an operation. If the operation returned an error, the Err method of SingleResult will return that error.