• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang mongo.CollectionFromName函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/juju/juju/mongo.CollectionFromName函数的典型用法代码示例。如果您正苦于以下问题:Golang CollectionFromName函数的具体用法?Golang CollectionFromName怎么用?Golang CollectionFromName使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了CollectionFromName函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: DumpColl

// DumpColl dumps the contents of a mongo database collection.
func DumpColl(db *mgo.Database, collName string) {
	if off {
		return
	}
	coll, closer := mongo.CollectionFromName(db, collName)
	defer closer()

	var results []interface{}
	err := coll.Find(nil).All(&results)
	if err != nil {
		panic(err)
	}
	called(2, collName)
	if len(results) == 0 {
		fmt.Println("no results found")
		return
	}
	for i, r := range results {
		output, err := json.MarshalIndent(r, "", " ")
		if err != nil {
			fmt.Print(err.Error())
		}
		fmt.Printf("Doc %d (%T): %s\n", i, r, output)
		fmt.Println("")
	}
	fmt.Println("")
}
开发者ID:Alyas6333,项目名称:xxx,代码行数:28,代码来源:xxx.go


示例2: isNotDead

func isNotDead(db *mgo.Database, collName string, id interface{}) (bool, error) {
	coll, closer := mongo.CollectionFromName(db, collName)
	defer closer()

	n, err := coll.Find(bson.D{{"_id", id}, {"life", bson.D{{"$ne", Dead}}}}).Count()
	return n == 1, err
}
开发者ID:jiasir,项目名称:juju,代码行数:7,代码来源:life.go


示例3: GetCollection

// GetCollection is part of the Database interface.
func (db *database) GetCollection(name string) (collection mongo.Collection, closer SessionCloser) {
	info, found := db.schema[name]
	if !found {
		logger.Errorf("using unknown collection %q", name)
	}

	// Copy session if necessary.
	if db.ownSession {
		collection = mongo.WrapCollection(db.raw.C(name))
		closer = dontCloseAnything
	} else {
		collection, closer = mongo.CollectionFromName(db.raw, name)
	}

	// Apply model filtering.
	if !info.global {
		collection = &modelStateCollection{
			WriteCollection: collection.Writeable(),
			modelUUID:       db.modelUUID,
		}
	}

	// Prevent layer-breaking.
	if !info.rawAccess {
		// TODO(fwereade): it would be nice to tweak the mongo.Collection
		// interface a bit to drop Writeable in this situation, but it's
		// not convenient yet.
	}
	return collection, closer
}
开发者ID:bac,项目名称:juju,代码行数:31,代码来源:database.go


示例4: imageMetadataDoc

func (s *imageStorage) imageMetadataDoc(envUUID, kind, series, arch string) (imageMetadataDoc, error) {
	var doc imageMetadataDoc
	id := fmt.Sprintf("%s-%s-%s-%s", envUUID, kind, series, arch)
	coll, closer := mongo.CollectionFromName(s.metadataCollection.Database, imagemetadataC)
	defer closer()
	err := coll.FindId(id).One(&doc)
	if err == mgo.ErrNotFound {
		return doc, errors.NotFoundf("%v image metadata", id)
	} else if err != nil {
		return doc, err
	}
	return doc, nil
}
开发者ID:imoapps,项目名称:juju,代码行数:13,代码来源:image.go


示例5: TestSaveAndFindAndDeleteMetadata

func (s *cloudImageMetadataSuite) TestSaveAndFindAndDeleteMetadata(c *gc.C) {
	metadata, err := s.client.List("", "", nil, nil, "", "")
	c.Assert(err, gc.ErrorMatches, "matching cloud image metadata not found")
	c.Assert(metadata, gc.HasLen, 0)

	//	check db too
	conn := s.State.MongoSession()
	coll, closer := mongo.CollectionFromName(conn.DB("juju"), "cloudimagemetadata")
	defer closer()

	before, err := coll.Count()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(before == 0, jc.IsTrue)

	imageId := "1"
	m := params.CloudImageMetadata{
		Source:          "custom",
		Stream:          "stream",
		Region:          "region",
		Series:          "trusty",
		Arch:            "arch",
		VirtType:        "virtType",
		RootStorageType: "rootStorageType",
		ImageId:         imageId,
	}

	err = s.client.Save([]params.CloudImageMetadata{m})
	c.Assert(err, jc.ErrorIsNil)

	added, err := s.client.List("", "", nil, nil, "", "")
	c.Assert(err, jc.ErrorIsNil)

	// m.Version would be deduced from m.Series
	m.Version = "14.04"
	c.Assert(added, jc.DeepEquals, []params.CloudImageMetadata{m})

	// make sure it's in db too
	after, err := coll.Count()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(after == 1, jc.IsTrue)

	err = s.client.Delete(imageId)
	c.Assert(err, jc.ErrorIsNil)
	// make sure it's no longer in db too
	afterDelete, err := coll.Count()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(afterDelete, gc.Equals, 0)
}
开发者ID:imoapps,项目名称:juju,代码行数:48,代码来源:cloudimagemetadata_test.go


示例6: listImageMetadataDocs

func (s *imageStorage) listImageMetadataDocs(envUUID, kind, series, arch string) ([]imageMetadataDoc, error) {
	coll, closer := mongo.CollectionFromName(s.metadataCollection.Database, imagemetadataC)
	defer closer()
	imageDocs := []imageMetadataDoc{}
	filter := bson.D{{"envuuid", envUUID}}
	if kind != "" {
		filter = append(filter, bson.DocElem{"kind", kind})
	}
	if series != "" {
		filter = append(filter, bson.DocElem{"series", series})
	}
	if arch != "" {
		filter = append(filter, bson.DocElem{"arch", arch})
	}
	err := coll.Find(filter).All(&imageDocs)
	return imageDocs, err
}
开发者ID:imoapps,项目名称:juju,代码行数:17,代码来源:image.go


示例7: SetUpTest

func (s *binaryStorageSuite) SetUpTest(c *gc.C) {
	s.BaseSuite.SetUpTest(c)
	s.mongo = &gitjujutesting.MgoInstance{}
	s.mongo.Start(nil)

	var err error
	var closer func()
	s.session, err = s.mongo.Dial()
	c.Assert(err, jc.ErrorIsNil)
	rs := blobstore.NewGridFS("blobstore", "blobstore", s.session)
	catalogue := s.session.DB("catalogue")
	s.managedStorage = blobstore.NewManagedStorage(catalogue, rs)
	s.metadataCollection, closer = mongo.CollectionFromName(catalogue, "binarymetadata")
	s.AddCleanup(func(*gc.C) { closer() })
	s.txnRunner = jujutxn.NewRunner(jujutxn.RunnerParams{Database: catalogue})
	s.storage = binarystorage.New("my-uuid", s.managedStorage, s.metadataCollection, s.txnRunner)
}
开发者ID:bac,项目名称:juju,代码行数:17,代码来源:binarystorage_test.go


示例8: initService

func (s *BakeryStorageSuite) initService(c *gc.C, enableExpiry bool) {
	store, err := bakerystorage.New(bakerystorage.Config{
		GetCollection: func() (mongo.Collection, func()) {
			return mongo.CollectionFromName(s.db, s.coll.Name)
		},
	})
	c.Assert(err, jc.ErrorIsNil)
	if enableExpiry {
		store = store.ExpireAt(time.Now())
	}
	s.store = store

	service, err := bakery.NewService(bakery.NewServiceParams{
		Location: "straya",
		Store:    s.store,
	})
	c.Assert(err, jc.ErrorIsNil)
	s.service = service
}
开发者ID:bac,项目名称:juju,代码行数:19,代码来源:bakerystorage_test.go


示例9: TestSaveAndFindMetadata

func (s *cloudImageMetadataSuite) TestSaveAndFindMetadata(c *gc.C) {
	metadata, err := s.client.List("", "", nil, nil, "", "")
	c.Assert(err, gc.ErrorMatches, "matching cloud image metadata not found")
	c.Assert(metadata, gc.HasLen, 0)

	//	check db too
	conn := s.State.MongoSession()
	coll, closer := mongo.CollectionFromName(conn.DB("juju"), "cloudimagemetadata")
	defer closer()

	before, err := coll.Count()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(before == 0, jc.IsTrue)

	m := params.CloudImageMetadata{
		Source:          "custom",
		Stream:          "stream",
		Region:          "region",
		Series:          "series",
		Arch:            "arch",
		VirtualType:     "virtType",
		RootStorageType: "rootStorageType",
		ImageId:         "1",
	}

	errs, err := s.client.Save([]params.CloudImageMetadata{m})
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(errs, gc.HasLen, 1)
	c.Assert(errs[0].Error, gc.IsNil)

	added, err := s.client.List("", "", nil, nil, "", "")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(added, jc.DeepEquals, []params.CloudImageMetadata{m})

	// make sure it's in db too
	after, err := coll.Count()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(after == 1, jc.IsTrue)
}
开发者ID:ktsakalozos,项目名称:juju,代码行数:39,代码来源:cloudimagemetadata_test.go


示例10: GetCollection

func (m *TestMongo) GetCollection(name string) (mongo.Collection, func()) {
	return mongo.CollectionFromName(m.database, name)
}
开发者ID:kat-co,项目名称:juju,代码行数:3,代码来源:image_test.go


示例11: isAlive

func isAlive(db *mgo.Database, collName string, id interface{}) (bool, error) {
	coll, closer := mongo.CollectionFromName(db, collName)
	defer closer()
	return isAliveWithSession(coll, id)
}
开发者ID:jiasir,项目名称:juju,代码行数:5,代码来源:life.go


示例12: getRawCollection

// getRawCollection returns the named mgo Collection. As no automatic
// environment filtering is performed by the returned collection it
// should be rarely used. getCollection() should be used in almost all
// cases.
func (st *State) getRawCollection(name string) (*mgo.Collection, func()) {
	collection, closer := mongo.CollectionFromName(st.db, name)
	return collection.Writeable().Underlying(), closer
}
开发者ID:Pankov404,项目名称:juju,代码行数:8,代码来源:collections.go


示例13: getCollection

// getCollection fetches a named collection using a new session if the
// database has previously been logged in to. It returns the
// collection and a closer function for the session.
//
// If the collection stores documents for multiple environments, the
// returned collection will automatically perform environment
// filtering where possible. See envStateCollection below.
func (st *State) getCollection(name string) (mongo.Collection, func()) {
	collection, closer := mongo.CollectionFromName(st.db, name)
	return newStateCollection(collection, st.EnvironUUID()), closer
}
开发者ID:Pankov404,项目名称:juju,代码行数:11,代码来源:collections.go



注:本文中的github.com/juju/juju/mongo.CollectionFromName函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang mongo.DefaultDialOpts函数代码示例发布时间:2022-05-23
下一篇:
Golang migration.SourcePrecheck函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap