如何过滤GAE查询?

I'm trying to save two records and then get the 2nd one. The issue is that the filter doesn't seem to work. Although I filter by Name ("Andrew W") I always get "Joe Citizen". The counter also indicates 2 records when it should be just one. This drives me crazy. See the full code below. The result prints counter 2 e2 {"Joe Citizen" "Manager" "2015-03-24 09:08:58.363929 +0000 UTC" ""}

package main
import (
    "fmt"
    "time"
    "net/http"

    "google.golang.org/appengine"
    "google.golang.org/appengine/datastore"
)


type Employee struct {
    Name     string
    Role     string
    HireDate time.Time
    Account  string
}
func init(){

    http.HandleFunc("/", handle)
}
func handle(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)

    e1 := Employee{
        Name:     "Joe Citizen",
        Role:     "Manager",
        HireDate: time.Now(),
    }

    _, err := datastore.Put(c, datastore.NewKey(c, "employee", "", 0, nil), &e1)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    panic(err)
        return
    }
    e1.Name = "Andrew W"

    _, err = datastore.Put(c, datastore.NewKey(c, "employee", "", 0, nil), &e1)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    panic(err)
        return
    }

    var e2 Employee
    q :=  datastore.NewQuery("employee")
    q.Filter("Name =", "Andrew W")
    cnt, err  := q.Count(c)
    if err !=nil{
        http.Error(w, err.Error(), http.StatusInternalServerError)
        panic(err)
        return
    }
    for t := q.Run(c); ; {  
        if _, err := t.Next(&e2); err != nil {
             http.Error(w, err.Error(), http.StatusInternalServerError)
            panic(err)
            return
        }
        break
    }   
    fmt.Fprintf(w, "counter %v e2 %q", cnt, e2)
}

The (first) problem is this:

q :=  datastore.NewQuery("employee")
q.Filter("Name =", "Andrew W")

Query.Filter() returns a derivative query with the filter you specified included. You have to store the return value and use it ongoing:

q := datastore.NewQuery("employee")
q = q.Filter("Name =", "Andrew W")

Or just one line:

q := datastore.NewQuery("employee").Filter("Name =", "Andrew W")

Note: Without this the query you execute would have no filters and therefore would return all previously saved entities of the kind "employee", where "Joe Citizen" might be the first one which you see printed.

For the first run you will most likely see 0 results. Note that since you don't use Ancestor queries, eventual consistency applies. The development SDK simulates the High replication datastore with its eventual consistency, and therefore the query following the Put() operations will not see the results.

If you put a small time.Sleep() before proceeding with the query, you will see the results you expect:

time.Sleep(time.Second)

var e2 Employee
q := datastore.NewQuery("employee").Filter("Name=", "Andrew W")
// Rest of your code...

Also note that running your code in the SDK you can simulate strong consistency by creating your context like this:

c, err := aetest.NewContext(&aetest.Options{StronglyConsistentDatastore: true})

But of course this is for testing purposes only, you can't do this in production.

If you want strongly consistent results, specify an ancestor key when creating the key, and use ancestor queries. An ancestor key is only required if you want strongly consistent results. If you're fine with a few seconds delay for the results to show up, you don't have to. Also note that the ancestor key does not have to be the key of an existing entity, it's just semantics. You can create any fictional key. Using the same (fictional) key to multiple entities will put them into the same entity group and ancestor queries on this group will be strongly consistent.

Often the ancestor key is an existing key, usually derived from the current user or account, because that can be created/computed easily and it holds/stores some additional information, but as noted above, it doesn't have to be.