本文整理汇总了Golang中labix/org/v2/mgo.Collection.FindId方法的典型用法代码示例。如果您正苦于以下问题:Golang Collection.FindId方法的具体用法?Golang Collection.FindId怎么用?Golang Collection.FindId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类labix/org/v2/mgo.Collection
的用法示例。
在下文中一共展示了Collection.FindId方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: articlePagingFunc
func articlePagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
article := &Article{}
if bson.IsObjectIdHex(first) {
if err := c.FindId(bson.ObjectIdHex(first)).One(article); err != nil {
return nil, err
}
query = bson.M{
"pub_time": bson.M{
"$gte": article.PubTime,
},
}
} else if bson.IsObjectIdHex(last) {
if err := c.FindId(bson.ObjectIdHex(last)).One(article); err != nil {
return nil, err
}
query = bson.M{
"pub_time": bson.M{
"$lte": article.PubTime,
},
}
}
return
}
示例2: txPagingFunc
func txPagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
tx := &Tx{}
if bson.IsObjectIdHex(first) {
if err := c.FindId(bson.ObjectIdHex(first)).One(tx); err != nil {
return nil, err
}
query = bson.M{
"time": bson.M{
"$gte": tx.Time,
},
}
} else if bson.IsObjectIdHex(last) {
if err := c.FindId(bson.ObjectIdHex(last)).One(tx); err != nil {
return nil, err
}
query = bson.M{
"time": bson.M{
"$lte": tx.Time,
},
}
}
return
}
示例3: loop
func (w *entityWatcher) loop(coll *mgo.Collection, key string) (err error) {
doc := &struct {
TxnRevno int64 `bson:"txn-revno"`
}{}
fields := D{{"txn-revno", 1}}
if err := coll.FindId(key).Select(fields).One(doc); err == mgo.ErrNotFound {
doc.TxnRevno = -1
} else if err != nil {
return err
}
in := make(chan watcher.Change)
w.st.watcher.Watch(coll.Name, key, doc.TxnRevno, in)
defer w.st.watcher.Unwatch(coll.Name, key, in)
out := w.out
for {
select {
case <-w.tomb.Dying():
return tomb.ErrDying
case <-w.st.watcher.Dead():
return watcher.MustErr(w.st.watcher)
case ch := <-in:
if _, ok := collect(ch, in, w.tomb.Dying()); !ok {
return tomb.ErrDying
}
out = w.out
case out <- struct{}{}:
out = nil
}
}
return nil
}
示例4: searchPagingFunc
func searchPagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
user := &Account{}
if len(first) > 0 {
if err := c.FindId(first).One(user); err != nil {
return nil, err
}
query = bson.M{
"lastlogin": bson.M{
"$gte": user.LastLogin,
},
}
} else if len(last) > 0 {
if err := c.FindId(last).One(user); err != nil {
return nil, err
}
query = bson.M{
"lastlogin": bson.M{
"$lte": user.LastLogin,
},
}
}
return
}
示例5: recordPagingFunc
func recordPagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
record := &Record{}
if bson.IsObjectIdHex(first) {
if err := c.FindId(bson.ObjectIdHex(first)).One(record); err != nil {
return nil, err
}
query = bson.M{
"starttime": bson.M{
"$gte": record.PubTime,
},
}
} else if bson.IsObjectIdHex(last) {
if err := c.FindId(bson.ObjectIdHex(last)).One(record); err != nil {
return nil, err
}
query = bson.M{
"starttime": bson.M{
"$lte": record.PubTime,
},
}
}
return
}
示例6: getCreatorName
func (r GameSessionResult) getCreatorName(c *mgo.Collection) GameSessionResult {
creatorName := MongoCreatorName{}
selection := bson.M{"creatorName": 1, "playtime": 1}
err := c.FindId(r.Session.ID).Select(selection).One(&creatorName)
if err != nil {
panic(err)
}
r.CreatorName = string(creatorName.CreatorName)
r.Playtime = int(creatorName.Playtime)
return r
}
示例7: getTxnRevno
// getTxnRevno returns the transaction revision number of the
// given key in the given collection. It is useful to enable
// a watcher.Watcher to be primed with the correct revision
// id.
func getTxnRevno(coll *mgo.Collection, key string) (int64, error) {
doc := &struct {
TxnRevno int64 `bson:"txn-revno"`
}{}
fields := bson.D{{"txn-revno", 1}}
if err := coll.FindId(key).Select(fields).One(doc); err == mgo.ErrNotFound {
return -1, nil
} else if err != nil {
return 0, err
}
return doc.TxnRevno, nil
}
示例8: main
func main() {
var (
mongoSession *mgo.Session
database *mgo.Database
collection *mgo.Collection
changeInfo *mgo.ChangeInfo
err error
)
if mongoSession, err = mgo.Dial("localhost"); err != nil {
panic(err)
}
database = mongoSession.DB("mgo_examples_04")
collection = database.C("todos")
var todo = Todo{
Id: bson.NewObjectId(),
Task: "Demo mgo",
Created: time.Now(),
}
// This is a shortcut to collection.Upsert(bson.M{"_id": todo.id}, &todo)
if changeInfo, err = collection.UpsertId(todo.Id, &todo); err != nil {
panic(err)
}
fmt.Printf("Todo: %# v", pretty.Formatter(todo))
fmt.Printf("Change Info: %# v", pretty.Formatter(changeInfo))
// START OMIT
var change = mgo.Change{
ReturnNew: true,
Update: bson.M{
"$set": bson.M{
"cp": time.Now(),
}}}
if changeInfo, err = collection.FindId(todo.Id).Apply(change, &todo); err != nil {
panic(err)
}
// END OMIT
fmt.Printf("Todo: %# v", pretty.Formatter(todo))
fmt.Printf("Change Info: %# v", pretty.Formatter(changeInfo))
}
示例9: friendsPagingFunc
func friendsPagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
user := &Account{}
var ids interface{}
if len(args) > 0 {
ids = args[0]
}
query = bson.M{
"_id": bson.M{
"$in": ids,
},
}
if len(first) > 0 {
if err := c.FindId(first).One(user); err != nil {
return nil, err
}
query = bson.M{
"props.score": bson.M{
"$gte": user.Props.Score,
},
"_id": bson.M{
"$in": ids,
},
}
} else if len(last) > 0 {
if err := c.FindId(last).One(user); err != nil {
return nil, err
}
query = bson.M{
"props.score": bson.M{
"$lte": user.Props.Score,
},
"_id": bson.M{
"$in": ids,
},
}
}
return
}
示例10: ensureDead
// ensureDead advances the specified entity's life status to Dead, if necessary.
// Preconditions can be supplied in assertOps; if the preconditions fail, the error
// will contain assertMsg. If the entity is not found, no error is returned.
func ensureDead(st *State, coll *mgo.Collection, id interface{}, desc string, assertOps []txn.Op, assertMsg string) (err error) {
defer trivial.ErrorContextf(&err, "cannot finish termination of %s %#v", desc, id)
ops := append(assertOps, txn.Op{
C: coll.Name,
Id: id,
Update: D{{"$set", D{{"life", Dead}}}},
})
if err = st.runner.Run(ops, "", nil); err == nil {
return nil
} else if err != txn.ErrAborted {
return err
}
var doc struct{ Life }
if err = coll.FindId(id).One(&doc); err == mgo.ErrNotFound {
return nil
} else if err != nil {
return err
} else if doc.Life != Dead {
return fmt.Errorf(assertMsg)
}
return nil
}
示例11: Get2
func Get2(collection *mgo.Collection, id bson.ObjectId, i interface{}) {
collection.FindId(id).One(i)
}
示例12: Get
func Get(collection *mgo.Collection, id string, i interface{}) {
collection.FindId(bson.ObjectIdHex(id)).One(i)
}