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

Date Objects not copied to new document when using Model Constructors & Strict: False Schema #7907

Closed
SamuelMaddox opened this issue Jun 17, 2019 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@SamuelMaddox
Copy link

SamuelMaddox commented Jun 17, 2019

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

Current behavior
Properties on objects that contain a value of type Date are not persisted to a document when passing an object into a model constructor, where the model is based off of a schema with the strict: false option set. This previously worked in version 5.5.1

Steps to reproduce.

const mongoose = require('mongoose');

// === Connect to MongoDB ===
const mongoURI = 'mongodb://localhost:27017';
const mongoOpts = {
  dbName: 'mongooseBug',
  useNewUrlParser: true,
  useCreateIndex: true,
}
mongoose.connect(mongoURI, mongoOpts);

// === Create Schema/Model
const schema = mongoose.Schema({}, { strict: false });
const SettingsModel = mongoose.model('Settings', schema);

// === Create Document ===
const settingsObject = {
  timestamp: new Date(),
  subDoc: {
    timestamp: new Date(),
  },
}
const settingsDocument = new SettingsModel(settingsObject);

// === Output Actual & Expected Documents
console.log('=== ACTUAL DOCUMENT ===')
console.log(settingsDocument);

console.log('\n=== EXPECTED DOCUMENT ===')
console.log({
  _id: '<AUTO GENERATED>',
  timestamp: settingsObject.timestamp,
  subDoc: {
    timestamp: settingsObject.subDoc.timestamp
  },
});

// === Save Document & Close MongoDB Connection ===
settingsDocument.save((err) => {
  mongoose.connection.close()
});

What is the expected behavior?
I expect the following to be logged out & saved to DB:

=== EXPECTED DOCUMENT ===
{ _id: '<AUTO GENERATED>',
  timestamp: 2019-06-17T23:02:07.711Z,
  subDoc: { timestamp: 2019-06-17T23:02:07.711Z } }

Instead I get the following:

=== ACTUAL DOCUMENT ===
{ _id: 5d081cb010dcaa21488d97d2 }

What are the versions of Node.js, Mongoose and MongoDB you are using?
Node: 10.7.0
Mongoose: 5.5.2 - 5.6.0. Version 5.5.1 provides the expected output
MongoDB: 4.0 & 3.6

@vkarpov15 vkarpov15 added this to the 5.6.1 milestone Jun 20, 2019
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jun 20, 2019
vkarpov15 added a commit that referenced this issue Jun 22, 2019
@vkarpov15
Copy link
Collaborator

Thanks for reporting this issue, we put a fix into master and it will be released with v5.6.1 next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants