Partial update of embedded document in mongoDB using mgo - mongodb

I have the following model:
type UserModel struct {
Id string `bson:"_id,omitempty"`
CreatedAt *time.Time `bson:"createdAt,omitempty"`
BasicInfo *UserBasicInfoModel `bson:"basicInfo,omitempty"`
}
// *Embedded document*
type UserBasicInfoModel struct {
FirstName *string `bson:"firstName,omitempty"`
LastName *string `bson:"lastName,omitempty"`
}
I am using pointers, in order to be able to distinguish between a missing value (nil) and default value (eg empty strings, false values etc). I also use omitempty to be able to do partial updates.
When I create a user I get the following (correct) response back:
"id": "aba19b45-5e84-55e0-84f8-90fad41712f6",
"createdAt": "2018-05-26T15:08:56.764453386+03:00",
"basicInfo": {
"firstName": "Initial first name",
"lastName": "Initial last name"
}
When I try to update the document though I have an issue.
I send the changes as a new UserModel, to only change the FirstName field in the embedded document like this:
newFirstName := "New Value"
UserModel{
BasicInfo: &UserBasicInfoModel{
FirstName: &newFirstName,
},
}
The code I use to do the update is the following:
UpdateId(id, bson.M{"$set": changes})
The response I get back is the following:
"id": "aba19b45-5e84-55e0-84f8-90fad41712f6",
"createdAt": "2018-05-26T12:08:56.764Z",
"basicInfo": {
"firstName": "New Value",
"lastName": null
}
The createdAt value is not null (as I expected) however the lastName value is null (which is not what I expected)
I would have expected to get back the following:
"id": "aba19b45-5e84-55e0-84f8-90fad41712f6",
"createdAt": "2018-05-26T12:08:56.764Z",
"basicInfo": {
"firstName": "New Value",
"lastName": "Initial last name"
}
What can I do to achieve a partial update in a subdocument using mgo?

First let's quickly explain your createdAt field. This is the value you save: 2018-05-26T15:08:56.764453386+03:00. Know that MongoDB stores dates with millisecond precision, and in UTC timezone. So this date when saved and retrieved from MongoDB becomes 2018-05-26T12:08:56.764Z, this is the "same" time instant, just in UTC zone and precision truncated to milliseconds.
Now on to updating embedded documents:
The short and unfortunate answer is that we can't do this directly with the mgo library and Go models.
Why?
When we use the ,omitempty option, and we leave some pointer fields at their zero value (that is, being nil), it's like if we were using a value whose type didn't even have those fields.
So in your example, if you only change the BasicInfo.FirstName field, and you use this value to update, it is equivalent to using these structures:
type UserModel struct {
Id string `bson:"_id,omitempty"`
BasicInfo *UserBasicInfoModel `bson:"basicInfo,omitempty"`
}
type UserBasicInfoModel struct {
FirstName *string `bson:"firstName,omitempty"`
}
So the effect of your issued update command will be the following:
db.users.update({_id: "aba19b45-5e84-55e0-84f8-90fad41712f6"},
{$set:{
"_id": "aba19b45-5e84-55e0-84f8-90fad41712f6",
"basicInfo": {
"firstName": "New Value"
}
}}
)
What does this mean? To set the _id to the same value (it won't change), and to set the basicInfo field to an embedded document which only has a single firstName property. This will erase the lastName field of the embedded basicInfo document. So when you unmarshal the document after the update into a value of your UserModel type, the LastName field will remain nil (because it is not present in MongoDB anymore).
What can we do?
Flatten the embedded document
One trivial solution is to not use an embedded document, but add fields of UserBasicInfoModel to UserModel:
type UserModel struct {
Id string `bson:"_id,omitempty"`
CreatedAt *time.Time `bson:"createdAt,omitempty"`
FirstName *string `bson:"firstName,omitempty"`
LastName *string `bson:"lastName,omitempty"`
}
Hybrid with ,inline option
This solution keeps the separate Go struct, but in MongoDB it will not be an embedded document (BasicInfo will be flattened just like in the previous example):
type UserModel struct {
Id string `bson:"_id,omitempty"`
CreatedAt *time.Time `bson:"createdAt,omitempty"`
BasicInfo UserBasicInfoModel `bson:"basicInfo,omitempty,inline"`
}
Note that BasicInfo needs to be a non-pointer if ,inline is used. This is not a problem, as we can leave it being an empty struct if its fields are not to be changed, since its fields are pointers, so leaving them nil will not change them.
Doing "manual" update
If you do need to use embedded document, the mgo library allows you to update specific fields of embedded documents, but then you have to "manually" construct the update document, like in this example:
c.UpdateId(Id, bson.M{"$set": bson.M{
"basicInfo.firstName": newFirstName,
}})
Yes, this isn't convenient at all. If you do need this many times with different types, you may create a utility function that uses reflection, iterates over the fields recursively, and assemble the update document from fields which are not nil. Then you could pass that dynamically generated update doc to UpdateId() for example.

Related

Update MongoDB array field in go

I have this data schema:
"person": { "name": "Neeraj", "hobbies": ["movies" ] }
This is the struct to insert the document in MongoDB
type Person struct {
Id primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Hobbies []string `json:"hobbies,omitempty" bson:"hobbies,omitempty"`
}
When PUT person API is called without the hobbies field, I would expect it to not update the hobbies field in DB. In this case when I parse the body, the struct has Hobbies set as null and because of 'omitempty' bson tag, it does not update the hobbies field in DB, which is fine.
filter := bson.D{{"_id", oid}}
update := bson.M{
"$set": record,
}
result, err := collection.UpdateOne(ctx, filter, update)
However, when PUT person API is called with the hobbies field set to an empty array, I would expect it to update the hobbies field and set it to empty. But in this case too, the record struct has hobbies field set to null, and it doesn't update the hobbies field.
How do I do this in go, so that my code caters to all of following update payload? I am using golang official mongo driver.
{
"name": "john" // Should not modify the hobbies field.
}
{
"name": "john",
"hobbies": [] // // Should update the hobbies field to an empty array
}
It sounds like you want to merge the person object based on what was provided in the request, whereas the MongoDB $set operator, "replaces the value of a field with the specified value." Because MongoDB doesn't understand how complex types should be merged, you'll have to specify the exact merging logic yourself.
filter := bson.M{"_id": oid}
set := bson.M{
"name": record.Name,
}
if (record.Hobbies) != nil {
set["hobbies"] = record.Hobbies
}
update := bson.M{"$set": set}

Go Mongo Driver Retrieve Schemaless Documents

While working with Mongo Go Driver I want to retrieve Schemaless Documents.
I am able to retrieve documents using bson.M json:",inline" bson:",inline"
But this adds extra "M" key in json when i try to Decode to a struct
type Product struct {
ID primitive.ObjectID `bson:"_id"`
ProductId string `bson:"product_id" json:"product_id"`
bson.M `json:",inline" bson:",inline"`
}
Output:-
{
"id":"<ObjectId>",
"M":{
"some":""
}
}
But instead what i want how it is stored in Mongo.
{
"id":"<ObjectId>",
"some":""
}
I cant use directly something like this as I want to cast it to struct to work with some properties
var pr bson.M
err := p.FindOne(ctx, &p.options,query, &pr)
How can I remove that extra key which is getting added while converting schemaless Documents from Mongo?
Do I need to explicitly overwrite MarshalJSON() or is there something provided using Tags?
How can I remove that extra key which is getting added while converting schemaless Documents from Mongo?
You can just define a field mapping name, which will be flattened when marshalled. For example:
type Product struct {
ID primitive.ObjectID `bson:"_id"`
ProductId string `bson:"product_id"`
Others bson.M `bson:",inline"`
}
When you decode a document, you'll see that it will include other fields without the Others name. For example if you have a document:
{
"_id": ObjectId("5e8d330de85566f5a0557ea4"),
"product_id": "foo",
"some": "x",
"more": "y"
}
doc := Product{}
err = cur.Decode(&doc)
fmt.Println(doc)
// Outputs
// {ObjectID("5e8d330de85566f5a0557ea4") foo map[more:y some:x]}
I cant use directly something like this as I want to cast it to struct to work with some properties
You can use this directly for a query predicate. For example:
// After decoding 'doc' to product
var result bson.M
err := collection.FindOne(context.TODO(), doc).Decode(&result)
Tested using MongoDB Go driver v1.3.2
UPDATED:
If you would like to return JSON, you could use bson.MarshalExtJSON(). This should be easier as well in terms of dealing with objects that don't exist in JSON. i.e. ObjectId. For example:
// After decoding 'doc' to product
ejson, err := bson.MarshalExtJSON(doc, true, false)
fmt.Println(string(ejson))

How to find and return a specific field from a Mongo collection?

Although I think it is a general question, I could not find a solution that matches my needs.
I have 2 Mongo collections. The 'users' collection and the second one 'dbInfos'.
Now, I have a template called 'Infos' and want the already existing fields in the Mongo collections to be presented to the user in input fields in case there is data in the collection. When no data is provided in the database yet, it should be empty.
So here is my code, which works fine until I want to capture the fields from the second collection.
Template.Infos.onRendered(function() {
$('#txtName').val(Meteor.user().profile.name);
$('#txtEmail').val(Meteor.user().emails[0].address);
});
These 2 work great.
But I don´t know how to query the infos from the collection 'dbInfos', which is not the 'users' collection. Obviously Meteor.user().country does not work, because it is not in the 'users' collection. Maybe a find({}) query? However, I don´t know how to write it.
$('#txtCountry').val( ***query function***);
Regarding the structure of 'dbInfos': Every object has an _id which is equal to the userId plus more fields like country, city etc...
{
"_id": "12345",
"country": "countryX",
"city": "cityY"
}
Additionally, how can I guarantee that nothing is presented, when the field in the collection is empty? Or is this automatic, because it will just return an empty field?
Edit
I now tried this:
dbInfos.find({},{'country': 1, '_id': 0})
I think this is the correct syntax to retrieve the country field and suppress the output of the _id field. But I only get [object Object] as a return.
you're missing the idea of a foreign key. each item in a collection needs a unique key, assigned by mongo (usually). so the key of your country info being the same as the userId is not correct, but you're close. instead, you can reference the userId like this:
{
"_id": "abc123",
"userId": "12345",
"country": "countryX",
"city": "cityY"
}
here, "abc123" is unique to that collection and assigned by mongo, and "12345" is the _id of some record in Meteor.users.
so you can find it like this (this would be on the client, and you would have already subscribed to DBInfos collection):
let userId = Meteor.userId();
let matchingInfos = DBInfos.find({userId: userId});
the first userId is the name of the field in the collection, the second is the local variable that came from the logged in user.
update:
ok, i think i see where you're getting tripped it. there's a difference between find() and findOne().
find() returns a cursor, and that might be where you're getting your [object object]. findOne() returns an actual object.
for both, the first argument is a filter, and the second argument is an options field. e.g.
let cursor = DBInfos.find({
userId: Meteor.userId()
},
{
fields: {
country: 1
}
});
this is going to:
find all records that belong to the logged in user
make only the country and _id fields available
make that data available in the form of a cursor
the cursor allows you to iterate over the results, but it is not a JSON object of your results. a cursor is handy if you want to use "{{#each}}" in the HTML, for example.
if you simply change the find() to a findOne():
let result = DBInfos.findOne({ /** and the rest **/
... now you actually have a JSON result object.
you can also do a combination of find/fetch, which works like a findOne():
let result = DBInfos.find({
userId: Meteor.userId()
},
{
fields: {
country: 1
}
}).fetch();
with that result, you can now get country:
let country = result.country;
btw, you don't need to use the options to get country. i've been assuming all this code is on the client (might be a bad assumption). so this will work to get the country as well:
let result = DBInfos.findOne({userId: Meteor.userId()});
let country = result.country;
what's going on here? it's just like above, but the result JSON might have more fields in it than just country and _id. (it depends on what was published).
i'll typically use the options field when doing a find() on the server, to limit what's being published to the client. on the client, if you just need to grab the country field, you don't really need to specify the options in that way.
in that options, you can also do things like sort the results. that can be handy on the client when you're going to iterate on a cursor and you want the results displayed in a certain order.
does all that make sense? is that what was tripping you up?

Data not fully retrieved while using mgo pipe function

I was using pipe function in mgo to retrieve data. My data structs are as follows.
type Company struct {
Id bson.ObjectId `bson:"_id,omitempty"`
CompanyName string
Slug string
CompanyUsers []CompanyUser
}
type CompanyUser struct {
UserName string
Email string
FullName string
}
I needed to check whether a given "UserName" is present under "CompanyUsers" in a "Company" with a given "Slug".
Both Slug and UserName are provided by the User.
Using Pipe function I successfully done the search, but the data is returned with an empty CompanyUsers array.
My query is as follows:
var companyResults []Company
pipeline := []bson.M{
{"$match": bson.M{"slug": slug}},
{"$unwind": "$companyusers"},
{"$match": bson.M{
"companyusers.username": username,
}},
}
err := c.Pipe(pipeline).All(&companyResults)
This provides me a search result as follows:
[{ObjectIdHex("573aa0fddd731711c94830ca") MyCompany companyslug [] }]
None of the data in CompanyUsers are retrieved. How can I solve this error?
Try making companyResults a []map[string]interface{} instead of a []Company to see what sort of a result you get. This helps to figure out what the structure of companyResults should be.
var companyResults []map[string]interface{}
You will see a result like,
[map[companyname:MyCompany slug:companyslug companyusers:map[username:username] _id:test]]
See that companyusers is infact a map not an array. This is because your usage of the $unwind stage. It deconstructs the array, outputting one document for each element in the array. See docs.
I agree with John Smith's answer, that you do not need pipelines at all. But following should give you the desired result.
type Result struct {
Id string `bson:"_id,omitempty"`
CompanyName string
Slug string
CompanyUsers CompanyUser
}
var companyResults []Result
I might be missing something, but why do you need Pipe for this?
You probably could do it easier:
query := bson.M{"slug": slug, "companyusers.username": username}
err := c.Find(query).All(&companyResults)
I'm fairly certain this would give you the same results.
However, the main problem is you didn't give the bson marshaller the fields name. Since the field in your database is companyusers and username you'll have to tell that to the marshaller.
type Company struct {
Id bson.ObjectId `bson:"_id,omitempty"`
CompanyName string
Slug string
CompanyUsers []CompanyUser `bson:"companyuser"`
}
type CompanyUser struct {
UserName string `bson:"username"`
Email string
FullName string
}
You probably want to fix that for the rest of the fields as well.

Mongo DB and Go - using dynamic data models

I am faced with a problem where I am not sure which path to take. So I am asking it here. I have an application where there can be products and there can be metadata for a product. These metadata can be created and deleted from the front end. So let's say, today a each product has two meta data (e.g. Name, Price) then tomorrow it can be three, four or more or even less than two. So this is dynamic. I am trying to represent the data as follows
Product =
{
"_id": mongo
"Name": string
"Description": string
"BasePrice": number
"CreatedBy": user mongo _id
"CreatedAt": timestamp
"ModifiedAt": timestamp
"MetaData": BSON object (having all the keys from ProductMetadata collection and their values. e.g. {"Category": "table ware", "Material": "oak wood, copper", "Length": 5.6})
}
ProductMetadata =
{
"_id": mongo
"Name": string (e.g. - "Category" or "Material" or "Height")
"Type": string (indicating what kind of value it can hold like string/integer/array. e.g. - "string")
"CreatedAt": timestamp
"ModifiedAt": timestamp
}
As you can see this is a pure dynamic situation so having a structure at the code level which represent the model is impossible.
My problem is, how do I implement such a thing using mgo and Go lang? If I need to use reflection then can some one please point me towards a good blog/tutorial where I can get a little bit more info. Or if you think that there is a fundamental problem in the approach of modeling the data then please correct me so that it can be easily implemented using Go.
In Python, it would not be a problem to implement this. I know that. But I am confused about the Go implementation.
Please help.
Thanks in advance
If Keys for metadata are unique, why not just use a map.
meaning your Product struct looks like:
struct Product {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string
Description string
... omitted other fields ...
MetaData map[string]map[string]interface{} // map of string -> map of string -> anything
}
If you can have more than one instance of a given meta data, ie: 2 categories, use a list:
struct Product {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string
Description string
... omitted other fields ...
MetaData []map[string]interface{} // list of maps of string -> anything
}