Skip to content
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

Updating Subdocuments containing { nested: { type: { } } } #7428

Closed
onlyarte opened this issue Jan 18, 2019 · 2 comments
Closed

Updating Subdocuments containing { nested: { type: { } } } #7428

onlyarte opened this issue Jan 18, 2019 · 2 comments

Comments

@onlyarte
Copy link

Do you want to request a feature or report a bug?

Report a bug.

What is the current behavior?

Document.save() does not save updates to a nested object of a Document's Subdocument if the nested object is defined as { nested: { type: { a: String }, default: { } } }.

If the current behavior is a bug, please provide the steps to reproduce.

Schemas:

const DataFeedMappingPair = new Schema({
  processing: {
    type: {
      formula: { type: String },
    },
    default: { },
  },
});

const DataFeedSchema = new Schema(
  {
    mapping: [DataFeedMappingPair],
  },
  { minimize: false },
);

Code:

const findByIdAndUpdateFormula = async (dataFeedId, mappingPairId, formula) => {
  const dataFeed = await DataFeed.findById(dataFeedId).exec();
  dataFeed.mapping.id(mappingPairId).processing.formula = formula;
  await dataFeed.save();
};

It does not save updates.

What is the expected behavior?

dataFeed.mapping.id(mappingPairId).processing.formula should be set to a provided value in the DB.

Please mention your node.js, mongoose and MongoDB version.

Node.js v10.6.0, mongoose@5.4.4, mongodb@3.1.10

@mhombach
Copy link

I'm not 100% sure but i think it could be that your processing-attribute gets the type of "mixed" (https://mongoosejs.com/docs/schematypes.html#mixed). So you would need to mark it as "modified" before saving, so mongoose knows that it needs to sve that too.
Can you check if my guess is right and give feedback?

@vkarpov15
Copy link
Collaborator

@mhombach is right, { nested: { type: { a: String }, default: { } } is the same as { nested: { type: {}, default: {} } }, see mixed docs. We have an issue open to track this here: #7181 so feel free to make comments there.

As a workaround, do this:

const DataFeedMappingPair = new Schema({
  processing: {
    type: new Schema({ // <-- wrap in `new Schema()` to avoid mongoose interpretting it as mixed
      formula: { type: String },
    }),
    default: { },
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants