當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Collection.Find方法代碼示例

本文整理匯總了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
}
開發者ID:voutasaurus,項目名稱:metamark,代碼行數:33,代碼來源:words.go

示例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
}
開發者ID:serash,項目名稱:YunYun,代碼行數:7,代碼來源:db.go

示例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)
}
開發者ID:revence27,項目名稱:Rhema,代碼行數:26,代碼來源:transfer.go

示例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)
}
開發者ID:kevinhuo88888,項目名稱:leanote,代碼行數:8,代碼來源:Mgo.go

示例5: Count

func Count(collection *mgo.Collection, q interface{}) int {
	cnt, err := collection.Find(q).Count()
	if err != nil {
		Err(err)
	}
	return cnt
}
開發者ID:kevinhuo88888,項目名稱:leanote,代碼行數:7,代碼來源:Mgo.go

示例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
}
開發者ID:netsharec,項目名稱:ironzebra,代碼行數:34,代碼來源:admin.go

示例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
}
開發者ID:serash,項目名稱:YunYun,代碼行數:7,代碼來源:db.go

示例8: assertSequenceExists

func assertSequenceExists(collection *mgo.Collection) {
	c, _ := collection.Find(nil).Count()
	if c == 0 {
		collection.Insert(&Counter{
			Seq: 0})
	}
}
開發者ID:relvinhas,項目名稱:ironzebra,代碼行數:7,代碼來源:post.go

示例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)
}
開發者ID:revence27,項目名稱:Rhema,代碼行數:27,代碼來源:transfer.go

示例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
}
開發者ID:DalianDragon,項目名稱:GO_LRS,代碼行數:27,代碼來源:LRSapi.go

示例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
}
開發者ID:nickstenning,項目名稱:router-archive,代碼行數:31,代碼來源:router.go

示例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
}
開發者ID:rcijov,項目名稱:go,代碼行數:9,代碼來源:account.go

示例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
}
開發者ID:serash,項目名稱:YunYun,代碼行數:8,代碼來源:db.go

示例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
}
開發者ID:galaek,項目名稱:trendings,代碼行數:9,代碼來源:trendings.go

示例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
}
開發者ID:serash,項目名稱:YunYun,代碼行數:8,代碼來源:db.go


注:本文中的labix/org/v2/mgo.Collection.Find方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。