Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are multiple ways you can handle this.</p> <p><strong>Using a map:</strong></p> <pre><code>var m bson.M err := collection.Find(nil).One(&amp;m) check(err) for key, value := range m { fmt.Println(key, value) } </code></pre> <p>Note that there's nothing special about <a href="http://labix.org/v2/mgo/bson#M" rel="noreferrer">bson.M</a> as far as mgo is concerned. It's just a <code>map[string]interface{}</code> type, and you can define your own map types and use them with mgo, even if they have a different value type.</p> <p><strong>Using a document slice:</strong></p> <p>The <a href="http://labix.org/v2/mgo/bson#D" rel="noreferrer">bson.D</a> is a slice that is internally known to mgo, and it exists both to offer a more efficient mechanism and to offer a way to preserve the ordering of keys, which is used by MongoDB in some circumstances (for example, when defining indexes).</p> <p>For example:</p> <pre><code>var d bson.D err := collection.Find(nil).One(&amp;d) check(err) for i, elem := range d { fmt.Println(elem.Name, elem.Value) } </code></pre> <p><strong>Using an ,inline map field</strong></p> <p>The <code>,inline</code> bson <a href="http://labix.org/v2/mgo/bson#Marshal" rel="noreferrer">flag</a> can also be used in a map field, so that you can have your cake and eat it too. In other words, it enables using a struct so that manipulating known fields is convenient, and at the same time allows dealing with unknown fields via the inline map.</p> <p>For example:</p> <pre><code>type Person struct { ID bson.ObjectId `bson:"_id,omitempty"` Name string Phone string Extra bson.M `bson:",inline"` } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload