1

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 ?

Amandeep kaur
  • 985
  • 3
  • 15
  • 35
  • With a map the order is not guaranted. I recommand you to define a struct as huge is it. – TZof May 25 '22 at 06:28

1 Answers1

0

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]()
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • What if I wanted to use this ordered map type for a field type in a struct? How can I do that ? Meaning how can I use this for map[string]map[string]interface{} ? – Amandeep kaur Jun 06 '22 at 11:05
  • @Amandeepkaur in your case, string would be the key, and the rest being the type of the value. I would define a type alias for the value type. – VonC Jun 06 '22 at 11:21
  • wk8/go-ordered-map is not woking, it says cant find package. you mean for map[string]map[string]interface{}, it would be m := orderedmap.NewOrderedMap[string, any]() ? – Amandeep kaur Jun 06 '22 at 11:25
  • @Amandeepkaur I would need to test it (but I am on my phone right now) – VonC Jun 06 '22 at 11:32
  • I have updated the post, could you please check that If its possible ? – Amandeep kaur Jun 06 '22 at 11:33
  • @Amandeepkaur Not tested, but you need a type alias with a Marshall/Unmarshall method, as in https://go.dev/play/p/LjOrp4k6lqf. – VonC Jun 06 '22 at 16:14
  • I dont need to to set the map keys like you did on playground. I know you added that for a complete example. But when I define a type alias then the data is not mapped from mongodb cursor to the defined type for a struct field. – Amandeep kaur Jun 07 '22 at 10:53
  • Also it returned the addresses of the map values. How to get the actual value ? – Amandeep kaur Jun 07 '22 at 11:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245390/discussion-between-vonc-and-amandeep-kaur). – VonC Jun 07 '22 at 13:49