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

child schemas set to false causes parent to get timestamps when parent timestamps set to false #7202

Closed
lineus opened this issue Oct 31, 2018 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.

Comments

@lineus
Copy link
Collaborator

lineus commented Oct 31, 2018

re: @YouriT 's comment on #7074

7202.js

#!/usr/bin/env node
'use strict';

const assert = require('assert');
const mongoose = require('mongoose');
const { Schema, connection} = mongoose;

const DB = '7202';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { family: 4, useNewUrlParser: true };

const subSchema = new Schema({}, { timestamps: false });
const schema = new Schema({ sub: subSchema }, { timestamps: false });

const Test = mongoose.model('test', schema);
const test = new Test({ sub: {} });

function strictEqual(a, b, msg) {
  try {
    assert.strictEqual(a, b, msg);
  } catch (e) {
    console.error(e.message);
  }
}

async function run() {
  assert.strictEqual(mongoose.version, '5.3.8');
  await mongoose.connect(URI, OPTS);
  await connection.dropDatabase();
  const doc = await Test.create(test);
  assert.ok(doc);
  strictEqual(doc.updatedAt, undefined, 'parent has createdAt set.');
  strictEqual(doc.createdAt, undefined, 'parent has updatedAt set.');
  strictEqual(doc.sub.createdAt, undefined, 'child has createdAt set.');
  strictEqual(doc.sub.updatedAt, undefined, 'child has updatedAt set.');
  await connection.close();
}

run();

Output:

issues: ./7202.js
parent has createdAt set.
parent has updatedAt set.
issues:
@lineus lineus added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 31, 2018
@YouriT
Copy link

YouriT commented Oct 31, 2018

@lineus I believe the only required modification is to expand the one you did in #7074.

lib/schema.js#L857

const childHasTimestamp = this.childSchemas.
  find(s => s.schema.options.timestamps != null);

if (!timestamps && !childHasTimestamp) {
  return;
}

Just replace

s => s.schema.options.timestamps != null

by

s => s.schema.options.timestamps === true || s.schema.options.timestamps == null

And the test will pass.

@lineus lineus changed the title parent & child schemas set to false causes parent to get timestamps child schemas set to false causes parent to get timestamps when parent timestamps not undefined Oct 31, 2018
@lineus lineus changed the title child schemas set to false causes parent to get timestamps when parent timestamps not undefined child schemas set to false causes parent to get timestamps when parent timestamps set to false Oct 31, 2018
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