question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow replacing nested fields

See original GitHub issue

Due to the use of moredots in modifyObject, it is currently impossible to remove properties of a nested object via the API (talking about this line: https://github.com/florianholzapfel/express-restify-mongoose/blob/master/src/operations.js#L204)

so if you have a schema:

User = mongoose.model('User', new mongoose.Schema({
    name: String,
    info: Object,
})

User.info is a generic object, so one would assume doing a PATCH /User/1 {info: {bmi: 33}} would update the info prop to the given value. But because moredots has no concept of the schema the above query gets converted to:

{ 'info.bmi': 33 }

This is a problem because if you have the following document:

{
    name: 'bob',
    info: {
         bmi: 33,
         weight: 100,
    }
}

There is no way to remove info.weight since posting with {info: {bmi: 22}} will just update the bmi instead of overwriting the entire info

Not sure if this was the intention or not, but it seems to me that it makes more sense to have PATCH be update only at the root level, and not recursive.

The current workaround is to specifically post with {info: {bmi: 22, weight: null}} which will overwrite that property to null which is not really ideal since it requires prior knowledge about the document and the schema

EDIT: Another workaround is if you make a middleware that does the above and sets unwanted values to undefined, since sending the object {info: {bmi: 22, weight: undefined}} does remove weight but you can’t json serialize undefined and you still have the issue of having to know about the structure of the data before updating

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
T-vKcommented, Jun 24, 2021

This is pretty simple actually.

This code, becomes:

const cleanBody = depopulate(req.body)

if (options.updateDeep) {
  cleanBody = moredots(cleanBody)
}

Where updateDeep defaults to true.

The work here is mostly in writing tests and making it clear that it disables nested protected and protected fields.

Thank you, this worked for me. Can you elaborate on the nested protected and protected fields topic and what exactly needs to be done in order to get this merged? I’m very interested in having this feature in the official express-restify-mongoose repo.

2reactions
Zertzcommented, Aug 7, 2016

Actually, rather than implementing yet another flag, I think we should perhaps be stricter and more “correct” in our use of HTTP verbs.

PATCH

  • Partially update a resource, this works now

POST

  • Create resources
  • Synonym to PATCH, whether we keep this or not is up for debate

PUT

  • Currently behavior is identical to PATCH, but it should entirely replace a resource instead

This is where most of the work is, which mostly boils down to:

  1. Skip the call to moredots
  2. Run the update without $set

I think that’s pretty much it, let me know if you want to give it a shot! If you’re not sure about the automated tests, send a PR and we’ll go from there 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spark Scala: How to Replace a Field in Deeply Nested ...
You could try using the DataFrame.withColumn() . It allows you to reference nested fields. You could add a new map column and drop...
Read more >
Manipulating Nested Data Just Got Easier in Apache Spark 3.1.1
Fortunately, the Column.withField method allows us to both add new fields or replace existing fields with the same name in our target struct....
Read more >
Specify nested and repeated columns in table schemas
To create a column with nested data, set the data type of the column to RECORD in the schema. A RECORD can be...
Read more >
Spark Replace NULL Values on DataFrame
In Spark, fill() function of DataFrameNaFunctions class is used to replace NULL values on the DataFrame column with either with zero(0), empty string,...
Read more >
Nested field type | Elasticsearch Guide [8.5] | Elastic
Instead, consider using the flattened data type, which maps an entire object as a single field and allows for simple searches over its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found