gpt4 book ai didi

mongodb - FindOneAndUpdate函数不更新数据库

转载 作者:行者123 更新时间:2023-12-02 11:41:55 26 4
gpt4 key购买 nike

我正在使用 Go 和 Mongo 创建 REST api。我对这些语言还很陌生。基本上,我调用此函数来更新数据库中的现有数据。它实际上并没有更新数据库中的任何内容。

func UpdateCompanyEndpoint(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
params := mux.Vars(request)
name, _ := params["name"]
var company Company
_ = json.NewDecoder(request.Body).Decode(&company)
filter := bson.D{{"name", name}}
fmt.Println(name)
update := bson.D{{"$set", bson.D{{"application", company.Application}}}}
collection := client.Database("RESTful").Collection("companies")
doc := collection.FindOneAndUpdate(
context.Background(),
filter,
update,
nil)
fmt.Println(doc)
}

数据库如下所示:

[
{
"name": "Test1",
"application": "Test1"
},
{
"name": "Test2",
"application": "Test2"
},
{
"name": "Test3",
"application": "Test3"
}
]

我在 http://localhost:8080/update/Test2 上调用 put 方法与:

{
"name": "Test2",
"application": "Test2update"
}

但是,它不会更新数据库中的任何内容。这是代码:https://github.com/jasonkim615/internship-db/blob/master/main.go

最佳答案

看来您正在尝试解码进入公司。我看不到公司的结构,但它保存的数据可能有助于回答你的问题。创建结构来模仿您的 json 结构,然后解码您的 json。这是一个例子(使用 UnMarshall,因为示例中使用静态字符串)用于流数据*写入器类型使用解码。

    package main 


import (
"fmt"
"encoding/json"
)

type person struct {
Name string `json:"Name"`
Age int `json:"Age"`
Alive bool `json:"Alive"`
}

func main(){
JSON:= `[{"Name":"somename","Age":29,"Alive":true},{"Name":"Tyrone","Age":39,"Alive":true}]`
//First Must Convert to slice of bytes
JSONBYTE:= []byte(JSON)
//Made New Slice To Hold Data From JSON
var people []person
//Converting JSON to Struct must give *Address
err := json.Unmarshal(JSONBYTE, &people)
//If
if err != nil {
fmt.Println(err)
}

for _, i := range people {
fmt.Printf("Person Name: %v\n", i.Name)
}
//Save To DB example only
// _, err = db.Collection(COLLECTION).InsertMany(context.Background(), people)
// if err != nil {
// log.Fatal(err)
}

这是一个使用 Decoder From 的示例 Parse JSON HTTP response using golang

    package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
)

type Example struct {
Type string `json:"type,omitempty"`
Subsets []Subset `json:"subsets,omitempty"`
}

type Subset struct {
Addresses []Address `json:"addresses,omitempty"`
}

type Address struct {
IP string `json:"IP,omitempty"`
}
func main() {

m := []byte(`{"type":"example","data": {"name": "abc","labels": {"key": "value"}},"subsets": [{"addresses": [{"ip": "192.168.103.178"}],"ports": [{"port": 80}]}]}`)

r := bytes.NewReader(m)
decoder := json.NewDecoder(r)

val := &Example{}
err := decoder.Decode(val)

if err != nil {
log.Fatal(err)
}

// If you want to read a response body
// decoder := json.NewDecoder(res.Body)
// err := decoder.Decode(val)

// Subsets is a slice so you must loop over it
for _, s := range val.Subsets {
// within Subsets, address is also a slice
// then you can access each IP from type Address
for _, a := range s.Addresses {
fmt.Println(a.IP)
}
}

}
//The output would be: 192.168.103.178

也是一个非常实用的工具,用于将 JSON 转换为 Struct https://mholt.github.io/json-to-go/

关于mongodb - FindOneAndUpdate函数不更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57895565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com