Preserve map order while unmarshalling bson data to a Golang map - mongodb

Mongo driver used: https://pkg.go.dev/go.mongodb.org/mongo-driver
I have some data saved in the mongodb like below:
{
"title" : "elem_1_3_title",
"list" : "elem_1_3_list"
}
When I receive this data using mongodb driver then it sorts the map in the alphabetical order:
cursor, err := collection.Aggregate(context.TODO(), pipeline)
if err != nil {
// handle err
}
pages := make([]map[string]interface{})
err = cursor.All(context.TODO(), &pages)
if err != nil {
// handle err
}
output:
{
"list" : "elem_1_3_list",
"title" : "elem_1_3_title"
}
updated:
type PageResp struct {
Id int `json:"_id,omitempty" bson:"_id,omitempty"`
Status int `json:"status" bson:"status"`
AddedSections []string `json:"added_sections" bson:"added_sections"`
Sections *orderedmap.OrderedMap `json:"sections,omitempty" bson:"sections,omitempty"`
}
The data from database is received in this struct & sections field is the map which needs to be ordered.
NOTE: I can not define structs for this as I have a very long list of fields & some new fields can be added in the future.
Is there any possible way to receive the same order which is saved under mongodb ?

You might need an ordered map in order to preserve order.
See for instance elliotchance/orderedmap described in "An Ordered Map in Go".
Or wk8/go-ordered-map with Go 1.18 and parameter types.
In both instances, that would replace your make([]map[string]interface{})
om := orderedmap.New[string, string]()

Related

cannot set type primitive.ObjectID to ObjectID

Getting this error while binding mongodb document to GO Struct using docstore collection iterator.
Database: Azure CosmosDB mongoDb API
Go Driver: Docstore
Code details:
Cosmos DB Constructors Code
client, err := c.newClient(ctx, true)
if err != nil {
log.Error("error connecting to mongodb cluster", zap.Error(err))
return nil, err
}
database := client.Database(c.dbName)
collection := database.Collection(c.collName)
return mongodocstore.OpenCollection(collection, "", nil)
Go struct for mapping with Mongo Db document
type utterance struct {
ID primitive.ObjectID `docstore:"_id,omitempty"`
User string `docstore:"user,omitempty"`
Locale string `docstore:"Locale,omitempty"`
Text string `docstore:"Text,omitempty"`
Source string `docstore:"Source,omitempty"`
Timestamp time.Time `docstore:"Timestamp,omitempty"`
DocstoreRevision interface{}
}
MondoDb Document
{
"_id" : ObjectId("60d5e18539864e948a8851a6"),
"User" : "auth0|6049b5ef5d79540071db6a0a",
"Locale" : "en_US",
"Text" : "Hi",
"Source" : "UTTERANCE_SOURCE_USER",
"Timestamp" : {
"$date" : 1624629637002
},
"DocstoreRevision" : "bf3b35d8-54ed-4a23-a08f-7d41b5c34085"
}
Method to call the docstore collection and iterate
i := s.collection.Query().Get(ctx)
defer i.Stop()
var results []*services.Utterance
for {
fmt.Println("for every document: ")
//var native utterance
doc := &utterance{}
err := i.Next(ctx, doc) //error at this line
if err == io.EOF {
break
} else if err != nil {
fmt.Println(“getting to this err block“)
return nil, err
}
u := doc.ToProto()
results = append(results, u)
}
Try setting the ID to a pointer type:
type utterance struct {
ID *primitive.ObjectID `docstore:"_id,omitempty"`
User string `docstore:"user,omitempty"`
Locale string `docstore:"Locale,omitempty"`
Text string `docstore:"Text,omitempty"`
Source string `docstore:"Source,omitempty"`
Timestamp time.Time `docstore:"Timestamp,omitempty"`
DocstoreRevision interface{}
}
Probably when you create the struct to be decoded the primitive.ObjectID is being initialized and the driver don't know to decode it.
And be careful. The docstore is a generic Document driver. It is not specific for MongoDB. So it probably don't work well with MongoDB specific types.
Reading the driver for mongodb https://pkg.go.dev/gocloud.dev/docstore/mongodocstore
There is a way to set what is stored in the _id field. I understand docstore don't know how to decode ObjectID from MongoDB.

Golang and MongoDB: DeleteMany with filter

I try to read and write and delete data from a Go application with the official mongodb driver for go (go.mongodb.org/mongo-driver).
Here is my struct I want to use:
Contact struct {
ID xid.ID `json:"contact_id" bson:"contact_id"`
SurName string `json:"surname" bson:"surname"`
PreName string `json:"prename" bson:"prename"`
}
// xid is https://github.com/rs/xid
I omit code to add to the collection as this is working find.
I can get a list of contacts with a specific contact_id using the following code (abbreviated):
filter := bson.D{}
cursor, err := contactCollection.Find(nil, filter)
for cur.Next(context.TODO()) {
...
}
This works and returns the documents. I thought about doing the same for delete or a matched get:
// delete - abbreviated
filter := bson.M{"contact_id": id}
result, _ := contactCollection.DeleteMany(nil, filter)
// result.DeletedCount is always 0, err is nil
if err != nil {
sendError(c, err) // helper function
return
}
c.JSON(200, gin.H{
"ok": true,
"message": fmt.Sprintf("deleted %d patients", result.DeletedCount),
}) // will be called, it is part of a webservice done with gin
// get complete
func Get(c *gin.Context) {
defer c.Done()
id := c.Param("id")
filter := bson.M{"contact_id": id}
cur, err := contactCollection.Find(nil, filter)
if err != nil {
sendError(c, err) // helper function
return
} // no error
contacts := make([]types.Contact, 0)
for cur.Next(context.TODO()) { // nothing returned
// create a value into which the single document can be decoded
var elem types.Contact
err := cur.Decode(&elem)
if err != nil {
sendError(c, err) // helper function
return
}
contacts = append(contacts, elem)
}
c.JSON(200, contacts)
}
Why does the same filter does not work on delete?
Edit: Insert code looks like this:
_, _ = contactCollection.InsertOne(context.TODO(), Contact{
ID: "abcdefg",
SurName: "Demo",
PreName: "on stackoverflow",
})
Contact.ID is of type xid.ID, which is a byte array:
type ID [rawLen]byte
So the insert code you provided where you use a string literal to specify the value for the ID field would be a compile-time error:
_, _ = contactCollection.InsertOne(context.TODO(), Contact{
ID: "abcdefg",
SurName: "Demo",
PreName: "on stackoverflow",
})
Later in your comments you clarified that the above insert code was just an example, and not how you actually do it. In your real code you unmarshal the contact (or its ID field) from a request.
xid.ID has its own unmarshaling logic, which might interpret the input data differently, and might result in an ID representing a different string value than your input. ID.UnmarshalJSON() defines how the string ID will be converted to xid.ID:
func (id *ID) UnmarshalJSON(b []byte) error {
s := string(b)
if s == "null" {
*id = nilID
return nil
}
return id.UnmarshalText(b[1 : len(b)-1])
}
As you can see, the first byte is cut off, and ID.UnmarshalText() does even more "magic" on it (check the source if you're interested).
All-in-all, to avoid such "transformations" happen in the background without your knowledge, use a simple string type for your ID, and do necessary conversions yourself wherever you need to store / transmit your ID.
For the ID Field, you should use the primitive.ObjectID provided by the bson package.
"go.mongodb.org/mongo-driver/bson/primitive"
ID primitive.ObjectID `json:"_id" bson:"_id"`

How to get ObjectId (_id) of my document in MGO mongo db driver for golang

I'm working with MGO (cause I didn't found anything better that it).
I'have played with it and have got some result but I don't understand how to get the _id (internal Mongo ObjectId) of document received?
For ex:
type FunnyNumber struct {
Value int
_id string
}
session, err := mgo.Dial("127.0.0.1:27017")
if err != nil {
panic(err)
}
defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
c := session.DB("m101").C("funnynumbers")
funnynumber := FunnyNumber{}
err = c.Find(bson.M{}).One(&funnynumber)
if err != nil {
log.Fatal(err)
}
fmt.Println("Id one:", funnynumber._id) // Nothing here? WHy? How to get it.
fmt.Println("Value one:", funnynumber.Value) // 62. It's OK!
Could someone help me, please? Ot where might I read some info about it? I haven't found anything in the MGO doc
Schema of my document is:
{ "_id" : ObjectId("50778ce69331a280cf4bcf90"), "value" : 62 }
Thanks!
Change _id variable to uppercase(ID) to make it exportable.
Use bson.ObjectID as its type.
Add tag for struct FunnyNumber Id variable.
field
Above three things should be done to get object Id value.
import "labix.org/v2/mgo/bson"
type FunnyNumber struct {
Value int `json:"value"`
Id bson.ObjectId `bson:"_id,omitempty"`` // only uppercase variables can be exported
}
Take a look at package BSON for more understanding on using bson tags when working with mongodb

Cannot Upsert only one value on struct interface from Mongo record [mgo]:golang

Basically I want update in one value from the mongodb document by given fully struct interface as a change parameter in collection.Upsert(selector,change). how we do this without lose other values into empty. Other(type,category.rerportby,createon,info) values should be keep on existing values only update plant and location values into PLANT07 and BAR)
NOTE: I want use completely Service Notification Struct Object for
do this.
DatabaseName:WO
CollectionName:SERVICE_NOTIFICATIONS
package models
//models.ServiceNotification
type ServiceNotification struct {
NotificationNo string `json:"notification_no" bson:"notification_no"`
Type string `json:"type" bson:"type"`
Category string `json:"category" bson:"category"`
Plant string `json:"plant" bson:"plant"`
Location string `json:"location" bson:"location"`
ReportedBy string `json:"reportedby" bson:"reportedby"`
Info map[string]interface{}`json:"info" bson:"info"`
SAPInfo SAPNotificationInfo `json:"sapinfo" bson:"sapinfo"`
CreateOn string `json:"createon" bson:"createon"`
UpdateOn string `json:"updateon" bson:"updateon"`
}
package main
func main(){
input := models.ServiceNotification{
NotificationNo:000120,
Plant:"Plant07",
Location:"BAR",
}
Change_ServiceNotification(input)
}
I want update plant and location by given complete struct interface to the mongo Upsert function. because I want to decide dynamically what should
update . But when I update plant and location other values going
to be LOST. in mongo record.
func Change_ServiceNotification(notification models.ServiceNotification) error {
session, err := commons.GetMongoSession()
if err != nil {
return errors.New("Cannot create mongodb session" + err.Error())
}
defer session.Close()
var col = session.DB(WO).C(SERVICE_NOTIFICATIONS)
selector := bson.M{"notification_no": notification.NotificationNo}
_, err = col.Upsert(selector, notification)
if err != nil {
errMsg := "Cannot update service notification " + err.Error()
return errors.New(errMsg)
}
return nil
}
Appreciate your help
Thanks in advance
You cannot do it this way, but you can use the $set operator of MongoDB (Skipping error checking):
input := Bson.M{
"$set": bson.M{
"plant": "Plant07",
// Further fields here...
}
}
selector := bson.M{"notification_no": notification.NotificationNo}
col.Upsert(selector, input)
This will update only the provided fields.

Golang mgo result into simple slice

I'm fairly new to both Go and MongoDB. Trying to select a single field from the DB and save it in an int slice without any avail.
userIDs := []int64{}
coll.Find(bson.M{"isdeleted": false}).Select(bson.M{"userid": 1}).All(&userIDs)
The above prints out an empty slice. However, if I create a struct with a single ID field that is int64 with marshalling then it works fine.
All I am trying to do is work with a simple slice containing IDs that I need instead of a struct with a single field. All help is appreciated.
Because mgo queries return documents, a few lines of code is required to accomplish the goal:
var result []struct{ UserID int64 `bson:"userid"` }
err := coll.Find(bson.M{"isdeleted": false}).Select(bson.M{"userid": 1}).All(&result)
if err != nil {
// handle error
}
userIDs := make([]int64, len(result))
for i := range result {
userIDs[i] = result.UserID
}