How to Write bson Array to ResponseWriter - mongodb

I am writing code in GoLang. As part of it, I generated bson array by querying a collection in MongoDB using github.com/mongodb/mongo-go-driver/mongo, github.com/mongodb/mongo-go-driver/bson. I need to write this response to http.ResponseWriter. When I attempt to do this using json.Marshal(BsonArrayReceived), response that is written to ResponseWriter has different document structure than JSON document structure stored in MongoDB. So, wanted to know the right way to write query results to ResponseWriter.
Let's say there are two documents that meet my query criteria - cat, dog
cat := bson.D{{"Animal", "Cat"}}
dog := bson.D{{"Animal", "Dog"}}
So resultant bson Array I am creating would be something like below
response := bson.A
response = append(response, cat)
response = append(response, dog)
My current code that did not work is below
writer.Header().Set("Content-Type", "application/json")
json.err := json.Marshal(response)
writer.Write(json)
Expected output would be
[{"Animal":"Cat"},{"Animal":"Dog"}]
Actual output I receive is
[{{"Key":"Animal"},{"Value":"Cat"}},{{"Key":"Animal"},{"Value":"Dog"}}]
So my question is how do I write to ResponseWriter so I preserve the JSON document array structure. I prefer not to use custom Marshal/UnMarshal as that would mean solution is specific and need changes if I change JSON structure

Use bons.M instead.
cat := bson.M{"Animal": "Cat"}
dog := bson.M{"Animal": "Dog"}
response := bson.A{}
response = append(response, cat)
response = append(response, dog)
writer.Header().Set("Content-Type", "application/json")
json, _ := json.Marshal(response)
writer.Write(json)

Related

Mongo Go Driver is getting interface conversion error when SetSort used

I would like to change order my documents in Mongo DB using Go. I have valid json string code and i can marshal it successfully in to map[string]int. The sample of this type just like:
[{year 1}, {lastupdated -1}]. The value presents order year field ascending and lastupdated field descending. This struct is aspect of MongoDB understands. Also I'm pass this data into bson.D type. Here is my code:
if queries["order"] != nil {
var unmarshalledOrder map[string]int
json.Unmarshal(queries["order"].([]byte), &unmarshalledOrder)
docRes := make(bson.D, 0)
for field, sort := range unmarshalledOrder {
docRes = append(docRes, bson.DocElem{field, sort})
}
log.Println(docRes)
}
When I print docRes, everthing goes well. But i pass the data to options.Sort function, the function throws interface conversion: interface {} is runtime.errorString, not string panic. Is it a bug on mongo go driver or am i wrong?
Can you post the code you've written which uses the driver? Based on the use of bson.DocElem, I think you're using mgo, but mgo's Query.Sort method takes strings, not documents (https://pkg.go.dev/github.com/globalsign/mgo?tab=doc#Query.Sort).

mgo $all query an array with an array and be case insensitive?

I have an array of ingredient names that is dynamic and provided per user. I'd like to match it to mongo documents where there is an array of objects called ingredients which has a property name. I've written a query (see below) which will take query parameters from the URL and will return all the documents that have all matching ingredient names, however this search is case sensitive and I'd like it not to be.
I've considered using bson.RegEx with Option: "i", however I'm not sure how to form this query or apply it to an array of strings.
Here is the case sensitive query:
// Check for ingredients, return all recipes that can be made using supplied ingredients
if qryPrms["ingredients"] != nil {
mongodQ["ingredients.name"] = bson.M{"$all": qryPrms["ingredients"]}
}
mongodQ is the bson.M I use to query the collection later in the code. Ideally I could apply RegEx to each element in qryPrms["ingredients"] so it would return closely matching ingredients like cheese would return swiss cheese as well. This is also a more general mongodb question I suppose when it comes to querying with a dynamic array.
I was able to accomplish this using a for loop to build a slice of type bson.RegEx.
if qryPrms["ingredients"] != nil {
var ingRegEx []bson.RegEx
for i := range qryPrms["ingredients"] {
ingRegEx = append(ingRegEx, bson.RegEx{Pattern: qryPrms["ingredients"][i], Options: "i"})
}
mongodQ["ingredients.name"] = bson.M{"$all": ingRegEx}
}

How to check if collection exists or not MongoDB Golang

I am new to GO language and I am using MongoDB with it. I am creating a backend for an application and its frontend on Angular 4. I want to check if collection exists or not.
Here is my code and I have checked it using nil.
collection := GetCollection("users")
fmt.Println("collection", collection)
if collection == nil {
fmt.Println("Collection is empty")
}
I have created a GetCollection function which return a collection when we pass it a collection name.
So when if there is no collection how can I check that if it exists or not?
I have tried many things but failed.
You may simply use the Database.CollectionNames() method which returns the collection names present in the given db. It returns a slice in which you have to check if your collection is listed.
sess := ... // obtain session
db := sess.DB("") // Get db, use db name if not given in connection url
names, err := db.CollectionNames()
if err != nil {
// Handle error
log.Printf("Failed to get coll names: %v", err)
return
}
// Simply search in the names slice, e.g.
for _, name := range names {
if name == "collectionToCheck" {
log.Printf("The collection exists!")
break
}
}
But as Neil Lunn wrote in his comments, you shouldn't need this. You should change your logic to use MongoDB not to rely on this check. Collections are created automatically if you try to insert a document, and querying from non-existing collections yields no error (and no result of course).

Obtain ObjectIdHex value from mgo query

I'm still new to go and while I see multiple questions on SO similar to this, I'm unable to reproduce the output some OP's had requested (this answer looking the closest).
I'm doing something fairly simple, I'm hitting a users collection in mongo and all I want to do is get the _id value back as a string. I'm going to eventually push these _id's up to NSQ but that's the brunt of my task.
var users []bson.M
err = sess.DB("db_name").C("users").Find(bson.M{}).All(&users)
if err != nil {
os.Exit(1)
}
for _, user := range users {
fmt.Printf("%+v \n", user["_id"])
}
Today this outputs:
ObjectIdHex("537f700b537461b70c5f0000")
ObjectIdHex("537f700b537461b70c600000")
ObjectIdHex("537f700b537461b70c610000")
ObjectIdHex("537f700b537461b70c620000")
I went through the bson#m docs and thought I was correctly using the map in order to extra the value. So I think, my query results in:
{"_id" : ObjectIdHex("Some_ID") }
but if ObjectIdHex("ID") is the value, how do I simply get the string within there.
So ideal output:
"537f700b537461b70c5f0000"
"537f700b537461b70c600000"
"537f700b537461b70c610000"
"537f700b537461b70c620000"
The value associated with key "_id" is of type bson.ObjectId which is simply a string.
bson.M is a type map[string]interface{}, so you need Type assertion to get the id as an ObjectId:
objid, ok := m["_id"].(ObjectId)
if !ok {
panic("Not ObjectId")
}
And the ObjectId has a ObjectId.Hex() method which returns exactly what you want: the object id as a "pure" hex string:
fmt.Println(objid.Hex())
Alternatives
objid can simply be converted to string because its underlying type is string. So you can use a number of further options to convert it to a hex string:
hexid := fmt.Sprintf("%x", string(objid))
If you just want to print it, you can do directly:
fmt.Printf("%x", string(objid))
Note: Converting it to string is important else the fmt package would call its String() method which results in a string like ObjectIdHex("537f700b537461b70c5f0000") and this is what would be converted to hex which is clearly not what you want.
Alternatively you can use the encoding/hex package and the hex.EncodeToString() function:
hexid := hex.EncodeToString([]byte(objid))

How to access to fetched data from mongodb in erlang?

I use mongodb-erlang driver for mongo db access in erlang. Some of my command execution:
34> {ok, Conn} = mongo:connect({localhost, 27017}).
{ok,{connection,{"localhost",27017},
<0.89.0>,false,infinity}}
35> {ok, Data} = mongo:do(safe, master, Conn, homeweb, fun() -> mongo:find_one(user, {apartmentId, 1}) end).
{ok,{{'_id',{<<79,180,252,18,220,119,245,66,215,79,71,61>>},
apartmentId,1.0,email,<<"e#mail.com">>,password,
<<"efe6398127928f1b2e9ef3207fb82663">>}}}
Data is a tuple.
For example in php array is returned from find request and I can get id with code like this: $id = $result['_id'];.
The question is: how to access to fetched from db data in Erlang?
By pattern matching. In this case, "Data" holds the result, so you might do something like:
1> {{'_id', {Id}, apartmentId, ApartmentId, email, Email, password, Password}} = Data.
{{'_id',{<<79,180,252,18,220,119,245,66,215,79,71,61>>},
apartmentId,1.0,email,<<"e#mail.com">>,password,
<<"efe6398127928f1b2e9ef3207fb82663">>}}
The words that start with an upper case will hold the values, so for example, you can print them:
2> ApartmentId.
1.0
3> Email.
<<"e#mail.com">>
4> Password.
<<"efe6398127928f1b2e9ef3207fb82663">>
5> Id.
<<79,180,252,18,220,119,245,66,215,79,71,61>>
EDIT: You are actually doing pattern matching when you run your query. Notice the {ok, Data} = on the left side of the = operator. This is effectively matching that the result is a tuple in the form {ok, Data} and since Data is unbound up to that point, it is assigned to the query result.
EDIT2: Since Data in this case is a bson(), you can refer to the erlang bson module (used as a dependency of the mongodb erlang driver): http://api.mongodb.org/erlang/bson/. There are specific functions you can use in this case, like bson:lookup/2 and bson:fields/1, passing as a parameter the bson() document (the result from mongodb:find_one/2)