I am working on a golang project. I am using MongoDB database. I need to get the all documents ids with the bulk update function. I am using mgo golang package with the code below:
package main
import (
"fmt"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2"
)
type QueryStruct struct{
Selector bson.M
Query bson.M
}
func main(){
session, err := mgo.Dial("127.0.0.1")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
var queries []QueryStruct
for i:=1; i<=400; i++{
var query QueryStruct
query.Selector = bson.M{"country":"US"}
query.Query = bson.M{"$set":bson.M{"serial_number":i}}
queries = append(queries, query)
}
c := session.DB("test").C("people")
bulk := c.Bulk()
for _, queryVal := range queries{
bulk.UpdateAll(queryVal.Selector, queryVal.Query)
}
data, err := bulk.Run()
fmt.Println(data)
fmt.Println(err)
}
I am getting the output like this:
&{117 117 false}
<nil>
I need to get the updated document ids to maintain the logs if any entry is not updated.
Can anybody suggest me how I can achieve this?