package mongodbAPI import ( "bytes" "encoding/json" ) type ( // InsertOneQuery represents the type used to make a single document insert into the database // swagger:model InsertOneQuery struct { Document interface{} `json:"document"` BsonID bool `json:"bson_id"` } // InsertManyQuery represents the type used to make an insert into the database // swagger:model InsertManyQuery struct { // list of documents interface to insert // required: true Documents interface{} `json:"documents"` } // Insert represents the insert response received back from the // mongo API. Insert struct { InsertedID string `json:"inserted_id,omitempty"` InsertedIDs []string `json:"inserted_ids,omitempty"` } ) // InsertOne method takes a mongodbAPI client as a pointer receiver and // calls the mongo using the /InsertOne URI func (d *Client) InsertOne(q *InsertOneQuery, table string) (Insert, error) { var resp Insert payload, err := json.Marshal(q) if err != nil { return resp, err } byt, err := d.post(payload, table+"/InsertOne") if err != nil { return resp, err } if err := json.NewDecoder(bytes.NewReader(byt)).Decode(&resp); err != nil { return resp, err } return resp, nil } // InsertMany method takes a mongodbAPI client as a pointer receiver and // calls the mongo using the /InsertMany URI func (d *Client) InsertMany(q *InsertManyQuery, table string) (Insert, error) { var resp Insert payload, err := json.Marshal(q) if err != nil { return resp, err } byt, err := d.post(payload, table+"/InsertMany") if err != nil { return resp, err } if err := json.NewDecoder(bytes.NewReader(byt)).Decode(&resp); err != nil { return resp, err } return resp, nil }