本文整理汇总了Golang中labix/org/v2/mgo.Collection.Insert方法的典型用法代码示例。如果您正苦于以下问题:Golang Collection.Insert方法的具体用法?Golang Collection.Insert怎么用?Golang Collection.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类labix/org/v2/mgo.Collection
的用法示例。
在下文中一共展示了Collection.Insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
示例2: AddUser
func AddUser(collection *mgo.Collection, name, username, password string) {
// Index
index := mgo.Index{
Key: []string{"username", "email"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := collection.EnsureIndex(index)
if err != nil {
panic(err)
}
bcryptPassword, _ := bcrypt.GenerateFromPassword(
[]byte(password), bcrypt.DefaultCost)
// Insert Dataz
err = collection.Insert(&models.User{Name: name, Username: username, Password: bcryptPassword})
if err != nil {
panic(err)
}
}
示例3: 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)
}
示例4: 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
}
示例5: assertSequenceExists
func assertSequenceExists(collection *mgo.Collection) {
c, _ := collection.Find(nil).Count()
if c == 0 {
collection.Insert(&Counter{
Seq: 0})
}
}
示例6: insertFakeAssets
func insertFakeAssets(c *mgo.Collection) {
assets := []Asset{
{
SlotID: 1,
AssetType: 2,
DivisionCode: "Truzzardi",
Environment: "preview",
},
{
SlotID: 1,
AssetType: 2,
DivisionCode: "Prada",
Environment: "preview",
},
{
SlotID: 1,
AssetType: 2,
DivisionCode: "Truzzardi",
Environment: "production",
},
{
SlotID: 1,
AssetType: 2,
DivisionCode: "VinDiesel",
Environment: "preview",
},
}
for _, asset := range assets {
err := c.Insert(asset)
if err != nil {
panic(err)
}
}
}
示例7: copyDirectoryDocument
func (fs *BFS) copyDirectoryDocument(d *Directory, newprefix, oldprefix, newname string, c *mgo.Collection) error {
// update parent path prefix with new prefix
_parent_path := d.Header.Parent
_parent_path = strings.Replace(_parent_path, oldprefix, newprefix, 1)
// update header info
uuid, err := makeUUID()
if err != nil {
return err
}
nw := time.Now()
dt := formatDatetime(&nw)
id := fmt.Sprintf("%s:%s", uuid, dt)
d.Header.Parent = _parent_path
if newname != "" {
err = fs.validateDirName(newname)
if err != nil {
return err
}
d.Header.Name = newname
}
d.Header.Created = dt
d.Id = id
// save to mongodb
err = c.Insert(&d)
if err != nil {
return err
}
return nil
}
示例8: AddAutoIncrementingField
func AddAutoIncrementingField(c *mgo.Collection) {
i, e := c.Find(bson.M{"_id": "users"}).Count()
if e != nil {
panic(fmt.Sprintf("Could not Add Auto Incrementing Field! collection:%s err:%v\n", c.FullName, e))
}
if i > 0 {
return
}
c.Insert(bson.M{"_id": "users", "seq": uint32(0)})
}
示例9: bulkInsert
func bulkInsert(col *mgo.Collection) {
msg := uuid.NewRandom().String()
_len := 100
bulk := make([]bson.M, _len)
for i := 0; i < _len; i++ {
bulk[i] = bson.M{"msg": msg, "counter": i}
}
err := col.Insert(bulk)
fmt.Print("Bulk Insert")
fmt.Print(err)
}
示例10: CreateUser
func CreateUser(w http.ResponseWriter, rew *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=UTF-8")
if rew.Method == "POST" {
var Users *mgo.Collection = MongoDB.C("users")
md5 := crypto.MD5.New()
md5.Write([]byte(rew.FormValue("password")))
passwordhash := fmt.Sprintf("%x", md5.Sum(nil))
err := Users.Insert(bson.M{"username": rew.FormValue("username"), "password": passwordhash})
if err == nil {
io.WriteString(w, "Success.")
} else {
io.WriteString(w, err.Error())
}
} else if rew.Method == "GET" {
Template(TemplateCreateUser).Execute(w, nil)
}
}
示例11: copyFileDocument
func (fs *BFS) copyFileDocument(f *File, newprefix, oldprefix, newname string, c *mgo.Collection) error {
// update parent path prefix with new prefix
_parent_path := f.Header.Parent
_parent_path = strings.Replace(_parent_path, oldprefix, newprefix, 1)
// update header info
uuid, err := makeUUID()
if err != nil {
return err
}
nw := time.Now()
dt := formatDatetime(&nw)
id := fmt.Sprintf("%s:%s", uuid, dt)
f.Header.Parent = _parent_path
f.Header.Created = dt
if newname != "" {
err = fs.validateFileName(newname)
if err != nil {
return err
}
f.Header.Name = newname
}
f.Id = id
// check if file has an attachment and copy if true
_attch_path := f.AHeader.Filepointer
if _attch_path != "" {
_attch_dir := path.Dir(_attch_path)
_new_attch_path := path.Join(_attch_dir, id)
_, err = exec.Command("cp", _attch_path, _new_attch_path).Output()
if err != nil {
return err
}
// set new attachment filepointer
f.AHeader.Filepointer = _new_attch_path
}
// save to mongodb
err = c.Insert(&f)
if err != nil {
return err
}
return nil
}
示例12: TxtImport
// 开始导入文本小说内容
func TxtImport(story *mgo.Collection, book *mgo.Collection, sysFile *sysFile) {
f, _ := os.OpenFile(sysFile.fName, os.O_RDONLY, 0666)
defer f.Close()
m := bufio.NewReader(f)
// char := 0
words := 0
lines := 0
for {
s, ok := m.ReadString('\n')
words = len(strings.Fields(s))
if words > 0 {
story.Insert(&Story{sysFile.fShortName, lines, s, words})
}
lines++
if ok != nil {
break
}
}
book.Insert(&Book{f.Name(), sysFile.fSize, sysFile.fMtime, lines})
}
示例13: SaveNewKotoba
func SaveNewKotoba(user_id bson.ObjectId, word string, ll string, diff int, hatsuon string, h_ string,
imi string, i_ string, c *mgo.Collection) (*MongoKotoba, error) {
timenow := time.Now().Local()
hours, _ := time.ParseDuration(DEFAULT_REVIEW_TIM)
r := timenow.Add(hours)
imis := SplitImi(imi)
hatsuons := SplitHatsuon(hatsuon)
labels := SplitLabels(ll)
// add meanings
newK := MongoKotoba{Id: bson.NewObjectId(),
User_id: user_id,
Goi: word,
Hatsuons: *hatsuons,
Hatsuon_: h_,
Imis: *imis,
Imi_: i_,
Labels: *labels,
Level: 2,
Review: r,
Difficulty: diff,
Unlocked: time.Now().Local()}
return &newK, c.Insert(&newK)
}
示例14: idempotentRecordVersion
func idempotentRecordVersion(bible *Bible, col *mgo.Collection) interface{} {
versions := col.Find(bson.M{
"name": bible.Version,
}).Limit(1)
cpt, e := versions.Count()
if e != nil {
panic(e)
}
if cpt > 0 {
ans := make(map[string]interface{})
e = versions.One(ans)
if e != nil {
panic(e)
}
return ans["_id"]
}
e = col.Insert(bson.M{
"name": bible.Version,
})
if e != nil {
panic(e)
}
return idempotentRecordVersion(bible, col)
}
示例15: Insert
func Insert(collection *mgo.Collection, i interface{}) bool {
err := collection.Insert(i)
return Err(err)
}