{"id":207649,"date":"2025-07-08T13:43:47","date_gmt":"2025-07-08T05:43:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207649\/"},"modified":"2025-07-08T13:43:47","modified_gmt":"2025-07-08T05:43:47","slug":"%e5%ae%98%e6%96%b9-mongo-go-driver-updateone-%e4%b8%ad-set-%e7%9a%84-bson-%e8%af%ad%e6%b3%95%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207649\/","title":{"rendered":"\u5b98\u65b9 mongo-go-driver UpdateOne \u4e2d $set \u7684 bson \u8bed\u6cd5\u662f\u4ec0\u4e48"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5b98\u65b9 mongo-go-driver UpdateOne \u4e2d $set \u7684 bson \u8bed\u6cd5\u662f\u4ec0\u4e48<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 11:45:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8Golang\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5b98\u65b9 mongo-go-driver UpdateOne \u4e2d $set \u7684 bson \u8bed\u6cd5\u662f\u4ec0\u4e48\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u719f\u6089\u5b98\u65b9 mongo-go-driver \u548c <code>updateone<\/code> \u7684\u6b63\u786e\u8bed\u6cd5\u3002 <\/p>\n<p>\u6211\u6700\u7b80\u5355\u7684\u5b8c\u6574\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>\uff08\u6ce8\u610f\uff1a\u4e3a\u4e86\u4f7f\u7528\u6b64\u4ee3\u7801\uff0c\u60a8\u9700\u8981\u66ff\u6362\u4e3a\u60a8\u81ea\u5df1\u7684\u7528\u6237\u540d\u548c\u670d\u52a1\u5668\u540d\uff0c\u5e76\u5c06\u767b\u5f55\u5bc6\u7801\u4f5c\u4e3a mongo_pw \u5bfc\u51fa\u5230\u73af\u5883\u4e2d\uff09\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"os\"\n\n    \"go.mongodb.org\/mongo-driver\/bson\"\n    \"go.mongodb.org\/mongo-driver\/bson\/primitive\"\n\n    \"go.mongodb.org\/mongo-driver\/mongo\"\n    \"go.mongodb.org\/mongo-driver\/mongo\/options\"\n)\n\ntype db struct {\n    user       string\n    server     string\n    database   string\n    collection string\n    client     *mongo.client\n    ctx        context.context\n}\n\nvar db = db{\n    user:       &lt;username&gt;,\n    server:     &lt;server_ip&gt;,\n    database:   \"test\",\n    collection: \"movies\",\n    ctx:        context.todo(),\n}\n\ntype movie struct {\n    id          primitive.objectid `bson:\"_id\" json:\"id\"`\n    name        string             `bson:\"name\" json:\"name\"`\n    description string             `bson:\"description\" json:\"description\"`\n}\n\nfunc main() {\n    if err := db.connect(); err != nil {\n        fmt.println(\"error: unable to connect\")\n        os.exit(1)\n    }\n    fmt.println(\"connected\")\n\n    \/\/ the code assumes the original entry for dunkirk is the following\n    \/\/ {\"name\":\"dunkirk\", \"description\":\"a world war 2 movie\"}\n    updatedmovie := movie{\n        name:        \"dunkirk\",\n        description: \"movie about the british evacuation in wwii\",\n    }\n\n    res, err := db.updatebyname(updatedmovie)\n    if err != nil {\n        fmt.println(\"error updating movie:\", err)\n        os.exit(1)\n    }\n    if res.matchedcount &lt; 1 {\n        fmt.println(\"error: update did not match any documents\")\n        os.exit(1)\n    }\n}\n\n\/\/ updatebyname changes the description for a movie identified by its name\nfunc (db *db) updatebyname(movie movie) (*mongo.updateresult, error) {\n    filter := bson.d{{\"name\", movie.name}}\n\n    res, err := db.client.database(db.database).collection(db.collection).updateone(\n        db.ctx,\n        filter,\n        movie,\n    )\n    if err != nil {\n        return nil, err\n    }\n    return res, nil\n}\n\n\/\/ connect assumes that the database password is stored in the\n\/\/ environment variable mongo_pw\nfunc (db *db) connect() error {\n    pw, ok := os.lookupenv(\"mongo_pw\")\n    if !ok {\n        fmt.println(\"error: unable to find mongo_pw in the environment\")\n        os.exit(1)\n    }\n    mongouri := fmt.sprintf(\"mongodb+srv:\/\/%s:%s@%s\", db.user, pw, db.server)\n\n    \/\/ set client options and verify connection\n    clientoptions := options.client().applyuri(mongouri)\n    client, err := mongo.connect(db.ctx, clientoptions)\n    if err != nil {\n        return err\n    }\n    err = client.ping(db.ctx, nil)\n    if err != nil {\n        return err\n    }\n\n    db.client = client\n    return nil\n}\n<\/pre>\n<p>\u5305\u6587\u6863\u4e2d <code>updateone<\/code> \u7684\u51fd\u6570\u7b7e\u540d\u662f\uff1a<\/p>\n<pre>func (coll *collection) updateone(ctx context.context, filter interface{}, \n    update interface{}, opts ...*options.updateoptions) (*updateresult, error)\n<\/pre>\n<p>\u56e0\u6b64\uff0c\u6211\u5728\u4e3a\u51fd\u6570\u521b\u5efa <code>update \u63a5\u53e3{}<\/code> \u53c2\u6570\u65f6\u663e\u7136\u72af\u4e86\u67d0\u79cd\u9519\u8bef\uff0c\u56e0\u4e3a\u6211\u9047\u5230\u4e86\u6b64\u9519\u8bef<\/p>\n<pre>error updating movie: update document must contain key beginning with '$'\n<\/pre>\n<p>\u8fd9\u91cc\u6700\u53d7\u6b22\u8fce\u7684\u7b54\u6848\u8868\u660e\u6211\u9700\u8981\u4f7f\u7528\u7c7b\u4f3c\u8fd9\u6837\u7684\u6587\u6863<\/p>\n<pre>{ $set: {\"Name\" : \"The Matrix\", \"Decription\" \"Neo and Trinity kick butt\" } }<\/pre>\n<p>\u4f46\u9010\u5b57\u8bb0\u5f55\uff0c\u8fd9\u4e0d\u4f1a\u5728 mongo-go-driver \u4e2d\u7f16\u8bd1\u3002<\/p>\n<p>\u6211\u60f3\u6211\u9700\u8981\u67d0\u79cd\u5f62\u5f0f\u7684 bson \u6587\u6863\u6765\u7b26\u5408 go \u8bed\u6cd5\u3002\u4e3a <code>update<\/code> \u521b\u5efa\u6b64 bson \u6587\u6863\u7684\u6700\u4f73\u548c\/\u6216\u6700\u6709\u6548\u7684\u8bed\u6cd5\u662f\u4ec0\u4e48\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u7ecf\u8fc7\u4e00\u6bb5\u65f6\u95f4\u7684\u5c1d\u8bd5\u540e\uff0c\u6211\u901a\u8fc7\u66f4\u6539\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684 <code>updatebyname<\/code> \u51fd\u6570\uff0c\u4f7f\u7528 \u89e3\u51b3\u4e86<strong>\u5927\u91cf\u5c1d\u8bd5\u548c\u9519\u8bef<\/strong>\u540e\u7684\u95ee\u9898\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\/\/ updatebyname changes the description for a movie identified by its name\nfunc (db *db) updatebyname(movie movie) (*mongo.updateresult, error) {\n    filter := bson.d{{\"name\", movie.name}}\n    update := bson.d{{\"$set\",\n        bson.d{\n            {\"description\", movie.description},\n        },\n    }}\n\n    res, err := db.client.database(db.database).collection(db.collection).updateone(\n        db.ctx,\n        filter,\n        update,\n    )\n    if err != nil {\n        return nil, err\n    }\n    return res, nil\n}\n<\/pre>\n<p>\u6ce8\u610f <code>bson.d{{$\"set\", ...<\/code> \u7684\u4f7f\u7528\u3002\u4e0d\u5e78\u7684\u662f\uff0cmongodb \u5b9e\u73b0 <code>bson<\/code> \u5305\u7684\u65b9\u5f0f\u4ecd\u7136\u6ca1\u6709\u901a\u8fc7 go-vet\u3002\u5982\u679c\u6709\u4eba\u6709\u8bc4\u8bba\u6765\u89e3\u51b3\u4e0b\u9762\u7684 lint \u51b2\u7a81\uff0c\u6211\u4eec\u5c06\u4e0d\u80dc\u611f\u6fc0\u3002<\/p>\n<pre>go.mongodb.org\/mongo-driver\/bson\/primitive.e composite literal uses unkeyed fields\n<\/pre>\n<p>\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u66ff\u6362\u6784\u9020<\/p>\n<pre>filter := bson.d{{\"name\", movie.name}}\n<\/pre>\n<p>\u4e0e<\/p>\n<pre>filter := bson.M{\"name\": movie.Name}\n<\/pre>\n<p>\u5982\u679c\u53c2\u6570\u987a\u5e8f\u4e0d\u91cd\u8981<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5b98\u65b9 mongo-go-driver UpdateOne \u4e2d $set \u7684 bson \u8bed\u6cd5\u662f\u4ec0\u4e48\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207649","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207649"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207649\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}