Comment 1 for bug 1474606

Revision history for this message
William Reade (fwereade) wrote : Re: Document replacements using $set are problematic

$set updates with structs work fine.

 db := s.MgoSuite.Session.DB("juju")
 coll := db.C("coll")
 runner := jujutxn.NewRunner(jujutxn.RunnerParams{Database: db})
 err := runner.RunTransaction([]txn.Op{{
  C: "coll",
  Id: "id",
  Insert: wtfDoc{"ping", "pong"},
 }})
 c.Assert(err, gc.IsNil)

 for i := 0; i < 3; i++ {
  var current map[string]interface{}
  err = coll.FindId("id").One(&current)
  c.Logf("doc looks like: %#v", current)
  err := runner.RunTransaction([]txn.Op{{
   C: "coll",
   Id: "id",
   Update: bson.D{{"$set", wtfDoc{fmt.Sprintf("ping%d", i), fmt.Sprintf("ping%d", i)}}},
  }})
  c.Assert(err, jc.ErrorIsNil)
 }

...produces:

doc looks like: map[string]interface {}{"foo":"ping", "bar":"pong", "_id":"id", "txn-revno":2, "txn-queue":[]interface {}{"55a756237f78f24806000005_44d766e0"}}
doc looks like: map[string]interface {}{"_id":"id", "bar":"ping0", "foo":"ping0", "txn-queue":[]interface {}{"55a756237f78f24806000006_89829a2c"}, "txn-revno":3}
doc looks like: map[string]interface {}{"_id":"id", "bar":"ping1", "foo":"ping1", "txn-queue":[]interface {}{"55a756237f78f24806000007_7b3c6b7b"}, "txn-revno":4}

...as expected.

You definitely don't want to use $set with a struct that defines txn-queue or txn-revno though...