Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2141

Add a recursive option to Map()

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Gone away
    • Icon: Minor - P4 Minor - P4
    • None
    • None
    • None
    • None

    Description

      Requesting a similar function to Map() or an option that enables Map() to recursively convert bson.D to a map.

      Due to ordering, bson.D is preferable to bson.M and we're using bson.D exclusively in the developing Go documentation. This type isn't ideal when demonstrating output to users since everything is prepended with "Key" and "Value".

      The Map() function provides a workaround for simple documents but breaks down on documents with arrays of documents, as it doesn't convert them.

      Example:

      	var result bson.D
      	err = coll.FindOne(context.TODO(), bson.D{{"title", "The Room"}}).Decode(&result)
      	// end findOne
      	if err != nil {
      		if err == mongo.ErrNoDocuments {
      			// This error means your query did not match any documents.
      			return
      		}
      		panic(err)
      	}
      	// use Map() to quickly convert bson.D to a map
      	output, err := json.MarshalIndent(result.Map(), "", "    ")
      	if err != nil {
      		panic(err)
      	}
      	fmt.Printf("%s\n", output)
      

      {
          "imdb": [
              {
                  "Key": "rating",
                  "Value": 3.5
              },
              {
                  "Key": "votes",
                  "Value": 25673
              },
              {
                  "Key": "id",
                  "Value": 368226
              }
          ],
          "languages": [
              "English"
          ],
         ...
          "title": "The Room",
      }
      

       

      Attachments

        Activity

          People

            benji.rewis@mongodb.com Benji Rewis (Inactive)
            nathan.leniz@mongodb.com Nathan Leniz
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: