当前位置: 首页>>代码示例>>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;未经允许,请勿转载。