package mongodbAPI import ( "bytes" "encoding/json" ) type ( // UpdateOneQuery represents the type used to update a single record in the database. // If the query results equal more than one value, this method will only update the first record. // swagger:model UpdateOneQuery struct { Query interface{} `json:"query"` Update interface{} `json:"update"` BsonID bool `json:"bson_id,omitempty"` Upsert bool `json:"upsert,omitempty"` } // UpdateManyQuery represents the type used to update multiple records in the database. // swagger:model UpdateManyQuery struct { Query interface{} `json:"query"` Update interface{} `json:"update"` BsonID bool `json:"bson_id,omitempty"` Upsert bool `json:"upsert,omitempty"` } // Update represents the payload structure returned from the Update // query to the mongo API Update struct { MatchedCount int `json:"matched_count"` UpsertedID string `json:"upserted_id"` } ) // UpdateOne method takes a mongodbAPI client as a pointer receiver and // calls the mongo using the /UpdateOne URI func (d *Client) UpdateOne(q *UpdateOneQuery, table string) (Update, error) { var resp Update payload, err := json.Marshal(q) if err != nil { return resp, err } byt, err := d.post(payload, table+"/UpdateOne") if err != nil { return resp, err } if err := json.NewDecoder(bytes.NewReader(byt)).Decode(&resp); err != nil { return resp, err } return resp, nil } // UpdateMany method takes a mongodbAPI client as a pointer receiver and // calls the mongo using the /UpdateMany URI func (d *Client) UpdateMany(q *UpdateManyQuery, table string) (Update, error) { var resp Update payload, err := json.Marshal(q) if err != nil { return resp, err } byt, err := d.post(payload, table+"/UpdateMany") if err != nil { return resp, err } if err := json.NewDecoder(bytes.NewReader(byt)).Decode(&resp); err != nil { return resp, err } return resp, nil }