【发布时间】:2018-10-19 09:26:38
【问题描述】:
我必须将mgo query MongoDB 转换为Go 的结果插入到文件中以获得the id of images
var path="/home/Medo/text.txt"
pipe := cc.Pipe([]bson.M{
{"$unwind": "$images"},
{"$group": bson.M{"_id": "null", "images":bson.M{"$push": "$images"}}},
{"$project": bson.M{"_id": 0}}})
response := []bson.M{}
errResponse := pipe.All(&response)
if errResponse != nil {
fmt.Println("error Response: ",errResponse)
}
fmt.Println(response) // to print for making sure that it is working
data, err := bson.Marshal(&response)
s:=string(data)
if err22 != nil {
fmt.Println("error insertion ", err22)
}
这是我必须创建文件并在其上写入的部分。
问题是当我在文本文件中得到查询结果时,我在每个值的最后一个值中得到了一个枚举值,例如:
id of images
23456678`0`
24578689`1`
23678654`2`
12890762`3`
76543890`4`
64744848`5`
所以对于每个值,我得到了一个最后排序的数字,我不知道如何,在从查询中获得响应后,我将 Bson 转换为 []Byte,然后转换为 String 但它让我在每个结果的最后一个得到枚举排序值
我想放弃那些012345
var _, errExistFile = os.Stat(path)
if os.IsNotExist(errExistFile) {
var file, errCreateFile = os.Create(path)
if isError(erro) {
return
}
defer file.Close()
}
fmt.Println("==> done creating file", path)
var file, errii = os.OpenFile(path, os.O_RDWR, 0644)
if isError(errii) {
return
}
defer file.Close()
// write some text line-by-line to file
_, erri := file.WriteString(s)
if isError(erri) {
return
}
erri = file.Sync()
if isError(erri) {
return
}
fmt.Println("==> done writing to file")
【问题讨论】:
标签: string mongodb go bson mgo