doutuanxiao4619 2019-04-07 23:29
浏览 946
已采纳

官方mongo-go-driver的UpdateOne中$ set的bson语法是什么

I am trying to get some familiarity with the official mongo-go-driver and ran into an hour-long delay trying to figure out the right syntax for UpdateOne.

My simplest full example follows (NOTE: in order to use this code you will need to substitute in your own user and server names as well as export the login password to the environment as MONGO_PW):

package main

import (
    "context"
    "fmt"
    "os"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"

    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

type DB struct {
    User       string
    Server     string
    Database   string
    Collection string
    Client     *mongo.Client
    Ctx        context.Context
}

var db = DB{
    User:       <username>,
    Server:     <server_IP>,
    Database:   "test",
    Collection: "movies",
    Ctx:        context.TODO(),
}

type Movie struct {
    ID          primitive.ObjectID `bson:"_id" json:"id"`
    Name        string             `bson:"name" json:"name"`
    Description string             `bson:"description" json:"description"`
}

func main() {
    if err := db.Connect(); err != nil {
        fmt.Println("error: unable to connect")
        os.Exit(1)
    }
    fmt.Println("connected")

    // The code assumes the original entry for dunkirk is the following
    // {"Name":"dunkirk", "Description":"a world war 2 movie"}
    updatedMovie := Movie{
        Name:        "dunkirk",
        Description: "movie about the british evacuation in WWII",
    }

    res, err := db.UpdateByName(updatedMovie)
    if err != nil {
        fmt.Println("error updating movie:", err)
        os.Exit(1)
    }
    if res.MatchedCount < 1 {
        fmt.Println("error: update did not match any documents")
        os.Exit(1)
    }
}

// UpdateByName changes the description for a movie identified by its name
func (db *DB) UpdateByName(movie Movie) (*mongo.UpdateResult, error) {
    filter := bson.D{{"name", movie.Name}}

    res, err := db.Client.Database(db.Database).Collection(db.Collection).UpdateOne(
        db.Ctx,
        filter,
        movie,
    )
    if err != nil {
        return nil, err
    }
    return res, nil
}

// Connect assumes that the database password is stored in the
// environment variable MONGO_PW
func (db *DB) Connect() error {
    pw, ok := os.LookupEnv("MONGO_PW")
    if !ok {
        fmt.Println("error: unable to find MONGO_PW in the environment")
        os.Exit(1)
    }
    mongoURI := fmt.Sprintf("mongodb+srv://%s:%s@%s", db.User, pw, db.Server)

    // Set client options and verify connection
    clientOptions := options.Client().ApplyURI(mongoURI)
    client, err := mongo.Connect(db.Ctx, clientOptions)
    if err != nil {
        return err
    }
    err = client.Ping(db.Ctx, nil)
    if err != nil {
        return err
    }

    db.Client = client
    return nil
}

The function signature for UpdateOne from the package docs is:

func (coll *Collection) UpdateOne(ctx context.Context, filter interface{}, 
    update interface{}, opts ...*options.UpdateOptions) (*UpdateResult, error)

So I am clearly making some sort of mistake in creating the update interface{} argument to the function because I am presented with this error

error updating movie: update document must contain key beginning with '$'

The most popular answer here shows that I need to use a document sort of like this

{ $set: {"Name" : "The Matrix", "Decription" "Neo and Trinity kick butt" } }

but taken verbatim this will not compile in the mongo-go-driver.

I think I need some form of a bson document to comply with the Go syntax. What is the best and/or most efficient syntax to create this bson document for the update?

  • 写回答

1条回答 默认 最新

  • dozoqn3347 2019-04-08 02:28
    关注

    After playing around with this for a little while longer I was able to solve the problem after A LOT OF TRIAL AND ERROR using the mongodb bson package by changing the UpdateByName function in my code above as follows:

    // UpdateByName changes the description for a movie identified by its name
    func (db *DB) UpdateByName(movie Movie) (*mongo.UpdateResult, error) {
        filter := bson.D{{"name", movie.Name}}
        update := bson.D{{"$set",
            bson.D{
                {"description", movie.Description},
            },
        }}
    
        res, err := db.Client.Database(db.Database).Collection(db.Collection).UpdateOne(
            db.Ctx,
            filter,
            update,
        )
        if err != nil {
            return nil, err
        }
        return res, nil
    }
    

    Note the use of bson.D{{$"set", .... It is unfortunate the way MongoDB has implemented the bson package this syntax still does not pass the go-vet. If anyone has a comment to fix the lint conflict below it would be appreciated.

    go.mongodb.org/mongo-driver/bson/primitive.E composite literal uses unkeyed fields
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的