本文整理汇总了Golang中labix/org/v2/mgo.Collection.Find方法的典型用法代码示例。如果您正苦于以下问题:Golang Collection.Find方法的具体用法?Golang Collection.Find怎么用?Golang Collection.Find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类labix/org/v2/mgo.Collection
的用法示例。
在下文中一共展示了Collection.Find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getwords
// getwords reads the words from the database into a local variable stored on the heap.
// This cuts down on database accesses, and encapsulates the database interactions,
// making the random word selection more portable.
func getwords(localwords []string, words *mgo.Collection) error {
// Within the words collection, Find documents with the word field, and then Select only
// the contents of that field, and return an Iterator.
iter := words.Find(bson.M{wordField: bson.M{"$exists": true}}).Select(bson.M{wordField: 1}).Limit(10000).Iter()
// Iterate through the results pulling out the strings.
tempresult := Word{}
for i := 0; iter.Next(&tempresult); i++ {
localwords[i] = tempresult.Word
//Debug: fmt.Print(tempresult.Word, ",")
}
/* // Debug
//fmt.Println("First: ", localwords[0])
//fmt.Println("Last: ", localwords[10000-1])
//Debug: fmt.Println(len(localwords))
*/
// We should check len(localwords) to catch errors
if len(localwords) < 1000 {
// some error - we need to close the iterator also.
// composite errors may occur.
}
// Close the iterator, return an error where applicable.
if err := iter.Close(); err != nil {
return err
}
return nil
}
示例2: GetNumberKnown
func GetNumberKnown(user_id bson.ObjectId, c *mgo.Collection) int {
n, err := c.Find(bson.M{"user_id": user_id, "level": 9}).Count()
if err != nil {
return 0
}
return n
}
示例3: idempotentRecordChapter
func idempotentRecordChapter(bid interface{}, chapter Chapter, col *mgo.Collection) interface{} {
them := col.Find(bson.M{
"position": chapter.Position,
"book": bid,
}).Limit(1)
cpt, e := them.Count()
if e != nil {
panic(e)
}
if cpt > 0 {
ans := make(map[string]interface{})
e = them.One(ans)
if e != nil {
panic(e)
}
return ans["_id"]
}
e = col.Insert(bson.M{
"position": chapter.Position,
"book": bid,
})
if e != nil {
panic(e)
}
return idempotentRecordChapter(bid, chapter, col)
}
示例4: ListByQWithFields
// 查询某些字段, q是查询条件, fields是字段名列表
func ListByQWithFields(collection *mgo.Collection, q bson.M, fields []string, i interface{}) {
selector := make(bson.M, len(fields))
for _, field := range fields {
selector[field] = true
}
collection.Find(q).Select(selector).All(i)
}
示例5: Count
func Count(collection *mgo.Collection, q interface{}) int {
cnt, err := collection.Find(q).Count()
if err != nil {
Err(err)
}
return cnt
}
示例6: addPost
func addPost(database *mgo.Database, collection *mgo.Collection, title, subtitle, slug, category, body, image string) (post models.Post) {
// Index
index := mgo.Index{
Key: []string{"shortid", "timestamp", "title", "tags"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := collection.EnsureIndex(index)
if err != nil {
panic(err)
}
// Insert Dataz
err = collection.Insert(&models.Post{
ShortID: models.GetNextSequence(database),
Title: title,
Category: category,
Slug: slug,
Subtitle: subtitle,
Body: body,
Timestamp: time.Now(),
Published: false})
if err != nil {
panic(err)
}
result := models.Post{}
collection.Find(bson.M{"title": title}).One(&result)
return result
}
示例7: FindKotoba
func FindKotoba(user_id bson.ObjectId, search string, c *mgo.Collection) *[]MongoKotoba {
kotoba := []MongoKotoba{}
c.Find(bson.M{"user_id": user_id, "$or": []interface{}{bson.M{"goi": bson.M{"$regex": ".*" + search + ".*"}},
bson.M{"imis.imi": bson.M{"$regex": ".*" + search + ".*"}},
bson.M{"hatsuons.hatsuon": bson.M{"$regex": ".*" + search + ".*"}}}}).All(&kotoba)
return &kotoba
}
示例8: assertSequenceExists
func assertSequenceExists(collection *mgo.Collection) {
c, _ := collection.Find(nil).Count()
if c == 0 {
collection.Insert(&Counter{
Seq: 0})
}
}
示例9: idempotentRecordBook
func idempotentRecordBook(vid interface{}, book Book, col *mgo.Collection) interface{} {
them := col.Find(bson.M{
"name": book.Name,
"version": vid,
}).Limit(1)
cpt, e := them.Count()
if e != nil {
panic(e)
}
if cpt > 0 {
ans := make(map[string]interface{})
e = them.One(ans)
if e != nil {
panic(e)
}
return ans["_id"]
}
e = col.Insert(bson.M{
"name": book.Name,
"position": book.Position,
"version": vid,
})
if e != nil {
panic(e)
}
return idempotentRecordBook(vid, book, col)
}
示例10: checkIdConflict
func checkIdConflict(w http.ResponseWriter, statementsC *mgo.Collection, statement Statement) int {
if statement.Id != "" {
// find any statements with this id
var result Statement
err := statementsC.Find(bson.M{"id": statement.Id}).One(&result)
if err != nil {
//fmt.Fprint(w, err)
return 0
}
// kludge the result has "stored" so add field for comparison
statement.Stored = result.Stored
// can't compare structs with arrays/maps
rj, _ := json.Marshal(result)
sj, _ := json.Marshal(statement)
if string(rj) != string(sj) {
return http.StatusConflict
} else {
//same no need to insert
return http.StatusNoContent
}
}
return 0
}
示例11: loadApplications
// loadApplications is a helper function which loads applications from the
// passed mongo collection and registers them as backends with the passed proxy
// mux.
func loadApplications(c *mgo.Collection, mux *triemux.Mux) (apps map[string]http.Handler) {
app := &Application{}
apps = make(map[string]http.Handler)
iter := c.Find(nil).Iter()
for iter.Next(&app) {
backendUrl, err := url.Parse(app.BackendURL)
if err != nil {
log.Printf("router: couldn't parse URL %s for backend %s "+
"(error: %v), skipping!", app.BackendURL, app.ApplicationId, err)
continue
}
proxy := httputil.NewSingleHostReverseProxy(backendUrl)
// Allow the proxy to keep more than the default (2) keepalive connections
// per upstream.
proxy.Transport = &http.Transport{MaxIdleConnsPerHost: 20}
apps[app.ApplicationId] = proxy
}
if err := iter.Err(); err != nil {
panic(err)
}
return
}
示例12: retrieve
// Retrieve Account into Mongo
func (a Account) retrieve(name string, s *mgo.Collection) Account {
result := Account{}
err := s.Find(bson.M{"name": name}).One(&result)
if err != nil {
panic(err)
}
return result
}
示例13: GetNumberReviewsNow
func GetNumberReviewsNow(user_id bson.ObjectId, c *mgo.Collection) int {
n, err := c.Find(bson.M{"user_id": user_id,
"review": bson.M{"$lte": time.Now()}}).Count()
if err != nil {
return 0
}
return n
}
示例14: GetTweetsVerified
// GetTweetsVerified obtains all the Tweets with Verified User from a collection
func GetTweetsVerified(colec mgo.Collection) []twittertypes.Tweet {
var tweetsVerified []twittertypes.Tweet
err := colec.Find(bson.M{"user.verified": true}).All(&tweetsVerified)
if err != nil {
panic(err)
}
return tweetsVerified
}
示例15: GetNumberMaster
func GetNumberMaster(user_id bson.ObjectId, c *mgo.Collection) int {
n, err := c.Find(bson.M{"user_id": user_id, "$or": []interface{}{bson.M{"level": 7},
bson.M{"level": 8}}}).Count()
if err != nil {
return 0
}
return n
}