-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DefaultValueHandling to Reduce JSON Size #534
Comments
@hacksparrow, could you please take a look? Thanks. |
@arukris-cdw We don't have this feature. It is understandable to not send the default values and fill them in the client-side representation of the model, but omitting in the write to the database is not supported in MongoDB, since MongoDB does not support default values. |
There is a discussion about this feature at https://jira.mongodb.org/browse/SERVER-24430. |
@hacksparrow In our implementation, Loopback is enforcing all datatypes to the JSON payload based on model json file. I think that is done using the datasource juggler, can this be used to enforce the settings we are looking for? thanks |
Hi Arun, my understanding is based on this statement, "I am looking for a solution to reduce the document size being written to mongo db." I am interpreting that as "I want MongoDB to fill in the defaults by itself". The feature cannot be implemented in LoopBack (or any framework) since MongoDB does not support default values as of now. However, if you mean, "I want LoopBack to fill in the details for me"; it is a supported feature in LoopBack 3, it is not yet supported in LoopBack 4. Which version of LoopBack are you using?
It is enabled at the model level in LB3, not supported in LB4, yet. |
Yes, I meant "I want LoopBack to fill in the details for me". Can you please let me know the steps to enable it in LB3 (based on below version). C:\ProgramData\IBM\MQSI\node_modules>npm ls loopback |
This is an example of a model's JSON file ( {
"name": "people",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true,
"default": "Krishna"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
} If no name is specified in the request body JSON, it will default to "Krishna" for all the models. When you execute |
My requirement is just the opposite.. I want the elements having default values in the json payload to be ignored/excluded. kindly go through below link, this is the same I want to achieve using loopback. (Members.Ignore capability) Thanks |
@arukris-cdw I see, so you are looking for the That will have to be implemented at the model level. The library you are referring looks like a client-side library, ignoring properties at the client-side makes sense. However, if we ignore properties while writing to the database, we will end up with missing data and incorrect query results. |
We are using Loopback as the client side application that enforces all datatypes and writes data to mongo db. Recently Biniam applied a solution in loopback to handle NumberDecimal datatypes which was not supported earlier. We give the json data as string to loopback and it converts it into NumberDecimal and writes to mongo db. Can we have a similar solution for my requirement. In the modeljson file if we have some properties that says Ignore, ignore all default values and insert the rest. ?. All application that are going to read the data from mongodb also will use the same json schema with defaults in it. Hence there will not be any missing data issue. Please let me know. If required we can have a quick call to discuss this problem (i work from Chicago US Central time 9am - 5pm CST). thanks |
There seems to be a big gap in the understanding somewhere, let's discuss on call. Monday, 9:00 am CT is good for me. Please share the Zoom or Skype or your preferable app's details at [email protected]. |
@bajtos @raymondfeng need your input here. CDW wants the ability to omit model properties with default values from being written to the database. While this may seem like a problematic feature, since it will affect the ability to query using those properties; it can actually be a desirable feature to have, if there are a thousands of properties with default values, and thousands such models, which will be queries only their IDs. Thoughts? |
I can see how this feature could be useful to reduce the amount of data stored in MongoDB. I would envision the following implementation in juggler or LB4 repository class (behind a feature flag):
I find this feature it problematic too. I am concerned about handling evolution of the database schema:
We need to carefully consider how to address these situations to ensure application works as expected. It may be enough to come up with a database migration guide and update the documentation to make it easy for users of the new feature flag to find this guide. For example, when changing the default value from X to Y, the migration can consists of two steps:
Implementation wise, I'd prefer to implement this feature as an extension (e.g. a Repository mixin on LB4 level). To enable such extension, we need |
@arukris-cdw in LB3 using Operation Hooks, you can programmatically remove the properties you want to omit. In module.exports = function(Movies) {
Movies.observe('before save', async function(ctx) {
if (ctx.instance.runtime === 0) ctx.instance.unsetAttribute('runtime');
});
}; Similarly you can call |
@hacksparrow . Thanks for getting back. As you are aware i have around 1800 attributes (with multiple nested json array attributes) and how to programmatically unset every attribute. Will you be able to provide me some dynamic function that can check the data for all attributes at different levels and do the unsetAttribute automatically?. thanks |
@arukris-cdw we are considering the possibilities. |
@arukris-cdw we have decided to go ahead and implement the ignore feature. |
@hacksparrow , thanks for considering this as a new feature. Just want to make sure we have the same understanding. Please let me know if you also agree to below explanation for this feature?. Ignore Feature: thanks |
We'll provide the ability to ignore properties if the value is the default; however, developers will have to set their own default values. We cannot hard-code You can do something like: "count": {
"type": "number",
"default": 0,
"defaultValueHandling": "ignore"
} Will this solve your problem? If no, then we'll have to reconsider the decision.
Can you elaborate this? |
That should be fine. We will set the default value for individual elements. The default value ignored can be changed by placing the DefaultValueAttribute on the property. thanks |
@arukris-cdw we have introduced a new property The PR has landed and will be available in the next minor version of |
I just released new versions of juggler today: 4.12.0 and 3.33.0. |
Closing as done. |
hi Team,
I am looking for a solution to reduce the document size being written to mongo db. Need to insert a new record to mongodb, and if the element value is same as default value in the json schema , those elements needs to be dropped from being inserted.
This is achieved in .NET using a DefaultValueHandling function, below link has some details around it.
https://www.newtonsoft.com/json/help/html/ReducingSerializedJSONSize.htm
Do we have similar feature in loopback?.
eg:
Payload:
Model Json:
In this example, "runtime" element is having a value of 0, which is also configured as default value in model json. In this case, i need the following to be inserted to MongoDB (without runtime element).
Expected JSON document in mongodb:
The text was updated successfully, but these errors were encountered: