How to insert unmarshalled xml to mongodb - mongodb

Here is my struct to unmarshall xml:
type parseStruct struct {
First string `xml:"CharCode"`
Second string `xml:"Value"`
}
type xmlParse struct {
Data []parseStruct `xml:"Valute"`
}
Result:
{[{AA 30.2070000} {AB 29.9378} {AC 30.0260} {AD 30.3702}]}
How should i insert this construction to mongoDB?
I'm trying it
It is my mongo struct
type result struct{
First string
Second string
}
Here is insert operation
for i, _ := range unm.Data {
collection.Insert(result{
Curr1: unm.Data[i].First,
Rate: unm.Data[i].Second,
})
fmt.Println("result", dataBatt.Data2[i].Rate, dataBatt.Data2[i].Curr1)
}
But if i need put to add to mongodb base other unmarshalled structure, similar to it but from other structure for example:
type parseSecondStruct struct {
First string `xml:"Char"`
Second string `xml:"Val"`
}
type xmlParse struct {
Data []parseSecondStruct `xml:"Valute"`
}
How should i paste 2 unrmashalled result in result struct and know where is result from 1 source and where is from 2.

Related

mgo golang doesnt update empty array using $set

struct and method:
type Group struct {
Id int64 `bson:"_id,omitempty"`
MediaFilterExceptionUserIds []int `bson:"media_filter_exception_user_ids,omitempty"`
}
func (g *Group) Save() error {
return DB.C("groups").UpdateId(g.Id, bson.M{"$set": &g})
}
func (g *Group) FindById() error {
return DB.C("groups").FindId(g.Id).One(&g)
}
trying to set media_filter_exception_user_ids to an empty []int{} and it doesn't work:
group := Group{}
group.FindById(123)
group.MediaFilterExceptionUserIds = []int{}
group.Save()
It works when there's an item inside the slice, but empty slice is not set.
MediaFilterExceptionUserIds type should change from []int to *[]int,
type Group struct {
Id int64 `bson:"_id,omitempty"`
MediaFilterExceptionUserIds *[]int `bson:"media_filter_exception_user_ids,omitempty"`
}
and then
group.MediaFilterExceptionUserIds = &[]int{}
will set it to an empty array in mongodb

Inserting struct in mongodb in Golang

I am trying to insert a struct in mongo database.
type SecretsStruct struct {
UserID string `bson:"userid" json:"userid"`
secretOne string `bson:"secret_one" json:secret_one`
secretTwo string `bson:"secret_two" json:secret_two`
secretThree string `bson:"secret_three" json:secret_three`
}
func (c *SecretsStruct) SetSecrets(userId string, encryptedKeys
[][]byte){
c.UserID = userId
c.secretOne = hex.EncodeToString(encryptedKeys[0])
c.secretTwo = hex.EncodeToString(encryptedKeys[1])
c.secretThree = hex.EncodeToString(encryptedKeys[2])
log.Printf("This is the c %s", c)
}
g := SecretsStruct{}
g.SetSecrets(userStruct.UserID, encryptedKeys)
err = secretCollection.Insert(g)
if err != nil {
panic(err)
}
I have tried inserting the byte arrays corresponding to the secrets but of no help. The result which gets populated to the corresponding insertion operation is :
{'_id': ObjectId('5b80117c118c660aaa0c87c2'),
'userid': 'eb19d220-ef13-43aa-8a7f-f78637718000'}
On the other hand, if I try to insert same data with a map but without struct.
secretCollection.Insert(bson.M{"userid": userStruct.UserID,
"secret_one": encryptedKeys[0],
"secret_two": encryptedKeys[1],
"secret_three": encryptedKeys[2]})
The insertion operation executes successfully.
You have to export your struct fields, so that another package (in this case mgo) can access them:
type SecretsStruct struct {
UserID string `bson:"userid" json:"userid"`
SecretOne string `bson:"secret_one" json:secret_one`
SecretTwo string `bson:"secret_two" json:secret_two`
SecretThree string `bson:"secret_three" json:secret_three`
}

Cannot save structure into mongodb with golang (only empty records created)

I have the following structure
type Result struct {
nid string
timestamp int64
hexhash string
addr string
}
which I want to save into mongodb:
I create it
r := Result{hex_id, int64(msg.timestamp.Unix()), hexhash, msg.addr.String()}
And test if it is created correctly:
fmt.Println(r)
Which gives me result I'm expecting:
{b8da3f19d1318af6879976c1eea66c78c48e1144 1421417252
65072917F19D7F4C4B54C9C66A3EB31F77012981 127.0.0.1:65290}
Then I save it into mongo:
h.c.Insert(r)
But in mongo i see only empty records:
db.data.find()
{ "_id" : ObjectId("54b91a268da6c829a412cd4d") }
The h in the code above defined as
type Handler struct {
storage map[string]Message
new_msg chan Message
new_inp chan Input
c *mgo.Collection
}
and
h.c = session.DB(DATABASE).C(COLLECTION)
The fileds of your record need to be public for other packages (like the MongoDB wrapper) to see them. Rename the fields like this:
type Result struct {
Nid string
Timestamp int64
Hexhash string
Addr string
}

MGO - empty results returned from Mongo that has results

I have a GOLANG struct as follows:
type OrgWhoAmI struct {
FriendlyName string `json:"friendlyName"`
RedemptionCode string `json:"redemptionCode"`
StartUrls []StartUrl `json:"startUrls"`
Status string `json:"status"`
Children []OrgChildren `json:"childrenReemptionCodes"`
}
type StartUrl struct {
DisplayName string `json:"displayName"`
URL string `json:"url"`
}
type OrgChildren struct {
FriendlyName string `json:"childFriendlyName"`
RedemptionCode string `json:"childRedemptionCode"`
}
I've created and successfully inserted records into a MongoDB collection (as I can see the results by querying Mongo with the CLI mongo program) - but when I query with MGO as follows, I get nothing:
func main() {
session, sessionErr := mgo.Dial("localhost")
defer session.Close()
// Query All
collection := session.DB("OrgData").C("orgWhoAmI")
var results []OrgWhoAmI
err = collection.Find(bson.M{}).All(&results)
if err != nil {
panic(err)
}
for _, res := range results {
fmt.Printf("Result: %s|%s\n", res.FriendlyName, res.RedemptionCode)
}
}
The results printed are:
Result: |
Result: |
Result: |
Result: |
If I ask for the count for records, I get the correct number, but all values for all fields are blank. Not sure what I'm missing here.
If you aren't creating them in go, it's probably not serializing the key names for you properly. The default for bson is to lowercase the keys, so you need to specify it if you want something else. Also note that you have a typo in OrgWhoAmI for json:"childrenReemptionCodes" (should be Redemption, I'm guessing). You can specify both bson and json separately if you want them to be different.
type OrgWhoAmI struct {
FriendlyName string `bson:"friendlyName" json:"friendlyName"`
RedemptionCode string `bson:"redemptionCode" json:"redemptionCode"`
StartUrls []StartUrl `bson:"startUrls" json:"startUrls"`
Status string `bson:"status" json:"status"`
Children []OrgChildren `bson:"childrenRedemptionCodes" json:"childrenRedemptionCodes"`
}
type StartUrl struct {
DisplayName string `bson:"displayName" json:"displayName"`
URL string `bson:"url" json:"url"`
}
type OrgChildren struct {
FriendlyName string `bson:"childFriendlyName" json:"childFriendlyName"`
RedemptionCode string `bson:"childRedemptionCode" json:"childRedemptionCode"`
}

How to insert a multidimensional array

want to save the data of the following format
{"_ibj_id":"1","url_id":'1',"url":{"0":"http://0.com","1":"http:://1.com"}}
Look at my code,
type db_list struct {
Url_id int
Url map[int]string
}
func list(table *mgo.Collection) {
var doc *goquery.Document
var e error
for i := 1628644; i > 1628643; i-- {
if doc, e = goquery.NewDocument("http://www.120ask.com/list/all/" + strconv.Itoa(i)); e != nil {
panic(e.Error())
}
var save_list db_list
save_list.Url_id = i
save_list.Url = make(map[int]string)
//fmt.Println("%s", doc.Text())
doc.Find(".q-quename").Each(func(n int, s *goquery.Selection) {
href, isTrue := s.Attr("href")
if isTrue {
save_list.Url[n] = href
fmt.Println("%D : %s", n, save_list.Url[n])
}
})
fmt.Println("%D", len(save_list.Url))
//save database
table.Insert(save_list)
}
}
The database will eventually save
Please view the picture in the annex, is to save the format of the data, save the URLvalue of the property 1
You're probably after the JSON Unmarshal function in encoding/json
{"_ibj_id":"1","url_id":'1',"url":{"0":"http://0.com","1":"http:://1.com"}} is technically invalid JSON due to the single-quotes around the url_id value( '1' should be "1") but other than that, it should map nicely to the following struct:
{
id string
url_id string
urls []string
}
But you may need to experiment with the types. According to the docs for the Unmarshal function, it will use the following Go types for each JSON type:
bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null
I'd highly recommend reading Andrew Gerrands Blog Post "JSON and Go".
It's unclear to me exactly what you are trying to do. One thing I notice though, is that in your desired format the keys for Url are string values where as in Go they are integers. You could try changing the type of Url to map[string]string and see if that solves your problem.