How to convert a slice of bson.M to a slice of struct ? when working with MongoDB - mongodb

I am working with MongoDB. I query a slice (list) of data then I want to convert it to a slice (list) of struct.
Here is my so far code:
type PostDBTagMongo struct {
IDPost int `bson:"id_post"`
Name string `bson:"name"`
}
var tagsList []model.PostDBTagMongo
// Query
ctx := context.Background()
cursor, _:= p.postTagCollection.Find(ctx, bson.M{"id_post": id})
var postTag []bson.M
if err = cursor.All(ctx, &postTag); err != nil {
log.Fatal(err)
}
postTagBytes, err := bson.Marshal(postTag)
bson.Unmarshal(postTagBytes, &tagsList)
Error at this line of code
postTagBytes, err := bson.Marshal(postTag)
Here is the error
{
"name": "WriteArray",
"parent": 0,
"action": "write",
....
}

Related

MongoDB Golang UpdateOne array

I made a method UpdateMessage to update the message, I can only update a few fields: message.body, message.update_at and message.modifications array
Below I attached a method that I wrote, I do NOT get an error, none at all, but the data are not updated, if I make a query to the database and check after calling this method, the data will not change.
What am I doing wrong when trying to update a database record?
Is there anything I can do to simplify the ChangeMessage method, namely to get rid of passing the old message to the new one by parameters? I need each message to keep the old version of the message in the message.modifications field if the data changes?
type Message struct {
ID string `json:"id" bson:"id"`
ChatID string `json:"chat_id" bson:"chat_id"`
FromID string `json:"from_id" bson:"from_id"`
CreateDate int64 `json:"create_date" bson:"create_date"`
Type string `json:"type" bson:"type"`
Media string `json:"media" bson:"media"`
Body string `json:"body" bson:"body"`
UpdateAt int64 `json:"update_at" bson:"update_at"`
Modifications []*Message `json:"modifications,omitempty" bson:"modifications"`
Viewed bool `json:"viewed" bson:"viewed"`
}
func (srv *Service) ChangeMessage(ctx context.Context, message *Message) error {
...
chat, err := srv.db.Chat.Find(ctx, &Chat{ID: message.ChatID})
msg, err := srv.db.Chat.FindMessage(ctx, &Message{
ID: message.ID,
ChatID: chat.ID,
FromID: uid,
})
....
message.Modifications = msg.Modifications
message.Modifications = append(message.Modifications, msg)
message.FromID = msg.FromID
message.CreateDate = msg.CreateDate
message.Type = msg.Type
message.Media = msg.Media
message.ChatID = msg.ChatID
message.Viewed = msg.Viewed
err = srv.db.Chat.UpdateMessage(ctx, message)
.......
}
func (m *Mongo) UpdateMessage(ctx context.Context, msg *Message) error {
...
update := bson.M{
"$set": bson.M{
"body": msg.Body,
"update_at": time.Now().UnixMilli(),
},
"$push": bson.M{
"modifications": msg.Modifications,
},
}
_, err := m.col.UpdateOne(ctx, bson.M{"id": msg.ID}, update, options.Update().SetUpsert(true))
....
}
Let me try to help here. I put together a small example to show you how to deal with your scenario (at least for the updating issue):
package main
import (
"context"
"fmt"
"strings"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
ctx context.Context
cancel context.CancelFunc
)
type Message struct {
ID string `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
}
func main() {
ctx, cancel = context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
// set MongoDB connection
clientOptions := options.Client().ApplyURI("mongodb://root:root#localhost:27017")
mongoClient, err := mongo.Connect(ctx, clientOptions)
if err != nil {
panic(err)
}
defer mongoClient.Disconnect(ctx)
// select collection
collection := mongoClient.Database("demodb").Collection("myCollection")
defer collection.Drop(ctx)
fmt.Println("original documents")
// insert some random msg
if _, err := collection.InsertMany(ctx, []interface{}{
Message{"1", "John Doe"},
Message{"2", "Suzy Lee"},
}); err != nil {
panic(err)
}
// get records
var messagesTmp []bson.M
cursorTmp, err := collection.Find(ctx, bson.M{})
if err != nil {
panic(err)
}
if err := cursorTmp.All(ctx, &messagesTmp); err != nil {
panic(err)
}
for _, v := range messagesTmp {
fmt.Println(v)
}
fmt.Println(strings.Repeat("#", 100))
// update
var updateRes *mongo.UpdateResult
if updateRes, err = collection.UpdateOne(ctx,
bson.M{"name": "John Doe"},
bson.D{
bson.E{
Key: "$set",
Value: bson.D{
bson.E{
Key: "name",
Value: "John Doe - Edited",
},
},
},
},
); err != nil {
panic(err)
}
fmt.Println("num docs updated:", updateRes.ModifiedCount)
// list all
fmt.Println(strings.Repeat("#", 100))
fmt.Println("new documents")
var messages []Message
cursor, err := collection.Find(ctx, bson.M{})
if err != nil {
panic(err)
}
if err := cursor.All(ctx, &messages); err != nil {
panic(err)
}
for _, v := range messages {
fmt.Println(v)
}
}
With this code, you should be able to successfully update your documents in MongoDB. To give it a try, make sure to run MongoDB at the right address with the credentials expected in the code.
About the document versioning, I think that you're already following a kind of Document Versioning Pattern. You can find more about it at this link.
Let me know if this clarifies your doubts or if you need something else, thanks!

Incorrect decoding in Mongo-go-driver

For example, I have this structure:
type Overview struct {
Symbol string `json:"Symbol,omitempty"`
AssetType string `json:"AssetType,omitempty"`
Name string `json:"Name,omitempty"`
Description string `json:"Description,omitempty"`
...
...
}
In addition to this, I have several other structures.
My function selects a suitable structure for Decode(), but when I try to get data from the database, I get the result in this form:
[
{
"Key": "_id",
"Value": "618aa6f2a64cb8105a9c7984"
},
{
"Key": "Symbol",
"Value": "IBM"
},
{
"Key": "FiscalYearEnd",
"Value": "December"
},
...
...
]
I expect a response in the form of my structure, but I get such an array. I tried declaring the structure for the response myself: var result models.Overview. After that, the problem disappeared, but this is not the solution for my problem
There is my function:
var (
models map[string]interface{}
)
func init() {
models = make(map[string]interface{})
models["Overview"] = models.Overview{}
models["Earnings"] = models.Earnings{}
...
...
}
func GetDbData(collection string, db *mongo.Database, filter bson.D) (interface{}, error) {
var result = models[collection] // Choosing a structure
res := db.Collection(collection).FindOne(context.TODO(), filter)
err := res.Decode(&result)
if err != nil {
return nil, err
}
return result, nil
}
I can't understand why this is happening, I hope that someone has already encountered this problem and will be able to help me
https://jira.mongodb.org/browse/GODRIVER-988
Another approach to solve this can be by first decoding it into bson.M type and then unmarshalling it to your struct. Yes, this is not optimal.
eg:
func GetMonthStatusByID(ctx context.Context, id string) (interface{}, error) {
var monthStatus interface{}
filter := bson.M\{"_id": id}
err := db.Collection("Months").FindOne(ctx, filter).Decode(&monthStatus)
return monthStatus, err
}
The above snippet should be changed to:
func GetMonthStatusByID(ctx context.Context, id string) (interface{}, error) {
var monthStatus interface{}
filter := bson.M\{"_id": id}
tempResult := bson.M{}
err := db.Collection("Months").FindOne(ctx, filter).Decode(&tempResult)
if err == nil {
obj, _ := json.Marshal(tempResult)
err= json.Unmarshal(obj, &monthStatus)
}
return monthStatus, err
}

How to find documents with multiple conditions using mongo-driver/mongo

While querying the below data, returned cursor is empty. while there is 100s of documents which satisfy the condition.
{
"_id": "5dd68c51a39809125944ffba",
"status": "success",
"balance": "0.000",
"request_params": {
"username": "test_user",
"service_code": "MR"
}
using below code
MongoDB driver "go.mongodb.org/mongo-driver/mongo"
func saveLog(data Log) bool {
mongo, err := openMongo()
if err != nil {
log.Println(err)
fmt.Println("Connection failed")
return false
} else {
LogCollection := mongo.Database(LogDb).Collection(CollectionLog)
insertedApi, err := LogCollection.InsertOne(context.TODO(), data)
if err != nil {
log.Println(err)
fmt.Println("Insert failed")
return false
} else {
log.Println(insertedApi.InsertedID)
return true
}
}
}
func parseLog() {
db, err := openMongo()
if err != nil {
fmt.Println(err)
fmt.Println("Connection failed")
return
} else {
logCollection := db.Database(LogDb).Collection(CollectionLog)
var results [] *Log
find := bson.D{{"status","success"},{"request_params",bson.D{{"username","test_user"}}}}
fmt.Println(find)
cur, err := logCollection.Find(context.TODO(), find)
if err != nil {
log.Fatal(err)
}else {
for cur.Next(context.TODO()) {
var elem Log
err := cur.Decode(&elem)
if err != nil {
fmt.Println("Parse error : ",err)
}
fmt.Println("Log : ",elem)
results = append(results, &elem)
}
}
}
}
Log write
saveLog(Log{"success","0.000",RequestParams{"test_user","MR"}})
Log read
parseLog()
Log struct
type Log struct {
Status string `bson:"status"`
Balance string `bson:"balance"`
RequestParams RequestParams `bson:"request_params"`
}
type RequestParams struct {
Username string `bson:"username"`
ServiceCode string `bson:"service_code"`
}
MongoDB data
status only is returning whole 8k documents
bson.D{{"status","success"}}
Isn't collection.Find() function the right one for it.
Shell command is returning documents correctly
db.log.find({"status":"success","request_params.username":"test_user"}).limit(10).pretty()
The issue here is because of the query filter. There is a difference between the following queries:
// Query A: {"status": "success", "request_params": {"username":"test_user"}}
find := bson.D{{"status","success"},{"request_params",bson.D{{"username","test_user"}}}}
// Query B: {"status": "success", "request_params.username":"test_user"}
find := bson.D{{"status","success"},{"request_params.username","test_user"}}
Query A means that you would like to match an exact document of request_params where the value object exactly equal to {"username":"test_user"}. None of the documents in your collection matches this criteria. The documents also contains {"service_code":"MR"}. While query B uses dot notation, which means that you would like to match request_params field where it contains a value of {"username":"test_user"}.
See also Query on Nested Field for more information.

How use update function of mongo-go-driver using struct

The update function of mongo-go-driver can be called like this.
filter := bson.D{"username", username}
update := bson.D{{"$set",
bson.D{
{"name", person.Name},
},
}}
result, err := collection.UpdateOne(ctx, filter, update)
type Person struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Username string `json:"username,omitempty" bson:"username,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
}
But, I need to call the update function using the person struct, without mentioning every field of person struct like this.
filter := bson.D{"username", username}
update := bson.D{{"$set", <<how to convert person struct to bson document?>>}}
result, err := collection.UpdateOne(ctx, filter, update)
How can I convert the person struct to bson document?
ReplaceOne I think is what you're after:
// Use it's ID to replace
filter := bson.M{"_id": existing.ID}
// Create a replacement object using the existing object
replacementObj := existing
replacementObj.SomeFieldToChange = "new-replacement-object"
updateResult, err := coll.ReplaceOne(context.Background(), filter, replacementObj)
assertNotErr(t, err)
assertEquals(t, 1, int(updateResult.ModifiedCount))
A note that ErrNotFound is no longer thrown as it was in mgo - you have to check the Modified/Upserted count.
You could do something like this:
func Update(person Person) error {
pByte, err := bson.Marshal(person)
if err != nil {
return err
}
var update bson.M
err = bson.Unmarshal(pByte, &update)
if err != nil {
return
}
// NOTE: filter and ctx(Context) should be already defined
_, err = collection.UpdateOne(ctx, filter, bson.D{{Key: "$set", Value: update}})
if err != nil {
return err
}
return nil
}
how about marshalling the person struct to bson?
package main
import (
"fmt"
"labix.org/v2/mgo/bson"
)
type person struct {
ID string `json:"_id,omitempty" bson:"_id,omitempty"`
Username string `json:"username,omitempty" bson:"username,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
}
func main() {
p := person{
ID: "id",
Username: "uname",
Name: "name",
}
var (
bm []byte
err error
)
if bm, err = bson.Marshal(p); err != nil {
panic(fmt.Errorf("can't marshal:%s", err))
}
update := bson.D{{"$set", bm}}
fmt.Printf("update is:%q\n", update)
}
run:
./sobson
update is:[{"$set" "4\x00\x00\x00\x02_id\x00\x03\x00\x00\x00id\x00\x02username\x00\x06\x00\x00\x00uname\x00\x02name\x00\x05\x00\x00\x00name\x00\x00"}]

MongoDB bson.M query

I am trying to query using bison all JSON data in MongoDB with two fields but am getting null as result.
{
"allowedList": [
{
"List": [
{
"allow": {
"ss": 1,
},
"Information": [
{
"Id": "Id1"
}
]
}
]
}
]
}
I was able to filter all using the MongoDB at command line using
db.slicedb.find({"allowedList.List.allow.ss":1,"allowedList.List.Information.nsiId":"Id-Id21"})
but using
query := bson.M{"allowedList.List.allow": bson.M{"ss": sst}, "allowedList.List.Information": bson.M{"Id": Id}}
sst and Id are integer and string input to the query function
err := db.C(COLLECTION).Find(query).All(&specificSlices)
but is not working, am getting null even though there are json data that match the two field. Can someone help point out what was wrong with my query?
Server and database config
type SliceDataAccess struct {
Server string
Database string
}
var db *mgo.Database
const (
COLLECTION = "slicedb"
)
Establish a connection to database
func (m *SliceDataAccess) Connect() {
session, err := mgo.DialWithTimeout(m.Server, 20*time.Second)
if err != nil {
log.Fatal(err)
}
db = session.DB(m.Database)
}
Structs fields
type InstanceInfo struct {
ID string `json:"nfId" bson:"_id"`
AllowedList []AllowedNssai `json:"allowedList" bson:"allowedList"`
}
type AllowedNssai struct {
List []AllowedSnssai `json:"List,omitempty" bson:"List"`
...
}
type AllowedSnssai struct {
Allow *Snssai `json:"allow,omitempty" bson:"allow"`
Information []NsiInformation `json:"Information,omitempty" bson:"Information"`
}
type NsiInformation struct {
Id string `json:"Id" bson:"Id"`
}
type Snssai struct {
Ss int32 `json:"sst" bson:"ss"`
}
Query function defined
func (m *SliceDataAccess) FindAll(sst int32, nsiId string ([]InstanceInfo, error) {
var specificSlices []InstanceInfo
query := bson.M{"allowedList.List.allow": bson.M{"ss": sst}, "allowedList.List.Information": bson.M{"Id": nsiId}}
err := db.C(COLLECTION).Find(query).All(&specificSlices)
if err != nil {
return specificSlices, err
}
return specificSlices, nil
}
HTTP handler function for request and response
func AvailabilityGet(w http.ResponseWriter, r *http.Request)
var slice InstanceInfo
err := json.NewDecoder(r.Body).Decode(&slice)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Object body not well decoded")
return
}
sst := slice.AllowedList[0].List[0].Allow.Sst
nsiId := slice.AllowedList[0].List[0].Information[0].Id
specificSlices, err := da.FindAll(sst, nsiId)
json.NewEncoder(w).Encode(specificSlices)
}
Attached is my the full go code i have done.
this worked
query := bson.M{"allowedNssaiList.allowedSnssaiList.allowedSnssai.sst": sst, "allowedNssaiList.allowedSnssaiList.nsiInformationList.nsiId": nsiId}