mgo Bulk() example

2,069 views
Skip to first unread message

S.

unread,
Oct 11, 2014, 12:57:23 PM10/11/14
to mgo-...@googlegroups.com
hello, 

could you please show me the example of new Bulk interface? 
i'm trying this one

docs := make([]interface{}, len(myarr))
for i := 0; i < len(myarr); i++ {
docs[i] = mystruct{myarr[i]}
}

......................

t.Bulk().Insert(docs...)
res, err := t.Bulk().Run()


 but it returns error - "Client Error: Remaining data too small for BSON object"

S.

unread,
Oct 11, 2014, 1:25:56 PM10/11/14
to mgo-...@googlegroups.com
well, I guess I gound the answer, the code should be shomething like that:


x := t.Bulk()
x.Insert(docs...)
res, err := x.Run()


but, I was hoping it will Insert documents no matter if the will be duplicated documents with unique index (just dropping it).
Instead, it have same behaviour as usual mgo.Insert(docs...) - no inserts and return the same error.
How could I make mgo to  bulk insert only unique documents and drop duplicates? 

S.

unread,
Oct 11, 2014, 2:42:00 PM10/11/14
to mgo-...@googlegroups.com
well, I finally figured it out, 

the final code is:

x := t.Bulk()
x.Unordered() //magic! :)
x.Insert(docs...)
res, err := x.Run()

thank you :)

Reply all
Reply to author
Forward
0 new messages