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

Unable to allow timestamps in the sub document when having _id field in the main document #13343

Closed
2 tasks done
fsevenm opened this issue Apr 29, 2023 · 1 comment · Fixed by #13416
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@fsevenm
Copy link

fsevenm commented Apr 29, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.0.0

Node.js version

12.22.12

MongoDB server version

4.4.17

Typescript version (if applicable)

No response

Description

I explain this based on the code in Step to Reproduce. It throws an error when setting timestamps: true in the level 2 sub document (dimensions) when having an _id in root document DataSchema for the tags field, regardless of the value. Here is the error:

/home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/helpers/timestamps/setupTimestamps.js:56
      this.constructor.base.now();
                            ^

TypeError: Cannot read property 'now' of undefined
    at SingleNested.schema.methods.initializeTimestamps (/home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/helpers/timestamps/setupTimestamps.js:56:29)
    at model.schema.methods.initializeTimestamps (/home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/helpers/timestamps/setupTimestamps.js:71:16)
    at /home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/model.js:3519:20
    at Array.map (<anonymous>)
    at /home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/model.js:3513:38
    at /home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/helpers/parallelLimit.js:49:16
    at /home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/model.js:3463:9
    at /home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/helpers/promiseOrCallback.js:26:18
    at /home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/document.js:2589:7
    at complete (/home/fsevenm/works/redacted/scripts/node/node_modules/mongoose/lib/document.js:2955:5)

I understand that I can just disable the _id directly in the sub schema ( ImageShema), but I think it's still an issue since it should allow disabling the _id that way as well, unless it's a known limitation. Not to mention, it actually still works if dimensions is an array (commented code).

Steps to Reproduce

const mongoose = require("mongoose");
const { Schema } = mongoose;

const mongoURI = "mongodb://root:root@127.0.0.1:27017/sample_collection";

(async () => {
  mongoose.connect(mongoURI);

  const ImageShema = new mongoose.Schema(
    {
      dimensions: {
        // type: [
        //   new mongoose.Schema(
        //     {
        //       width: { type: Number, required: true },
        //       height: { type: Number, required: true },
        //     },
        //     { timestamps: true }
        //   ),
        // ],
        type: new mongoose.Schema(
	      {
	        width: { type: Number, required: true },
	        height: { type: Number, required: true },
	      },
	      { timestamps: true }
        ),
        required: true,
      },
    },
    { timestamps: true }
  );

  const DataSchema = new mongoose.Schema({
    tags: { type: ImageShema, required: false, _id: false },
  });

  const DataModel = mongoose.model("sample_data", DataSchema);

  await DataModel.insertMany([
    {
      tags: {
        // dimensions: [
        //   {
        //     width: 960,
        //     height: 589,
        //   },
        // ],
        dimensions: {
          width: 960,
          height: 589,
        },
      },
    },
  ]);

  mongoose.connection.close();
})();

Expected Behavior

Should be able to have timestamps: true for dimensions, no matter we set the _id for ImageSchema in the schema itself or in the root schema declaration, as well as array or not array field.

@vkarpov15 vkarpov15 added this to the 6.11.1 milestone May 2, 2023
@IslandRhythms
Copy link
Collaborator

Had to change the connection string. I don't get the error on mongoose 6 but I do get it on mongoose 7.1.0

const mongoose = require("mongoose");
const { Schema } = mongoose;

const mongoURI = "mongodb://localhost:27017";

(async () => {
  mongoose.connect(mongoURI);

  const ImageShema = new mongoose.Schema(
    {
      dimensions: {
        // type: [
        //   new mongoose.Schema(
        //     {
        //       width: { type: Number, required: true },
        //       height: { type: Number, required: true },
        //     },
        //     { timestamps: true }
        //   ),
        // ],
        type: new mongoose.Schema(
	      {
	        width: { type: Number, required: true },
	        height: { type: Number, required: true },
	      },
	      { timestamps: true }
        ),
        required: true,
      },
    },
    { timestamps: true }
  );

  const DataSchema = new mongoose.Schema({
    tags: { type: ImageShema, required: false, _id: false },
  });

  const DataModel = mongoose.model("sample_data", DataSchema);

  await DataModel.insertMany([
    {
      tags: {
        // dimensions: [
        //   {
        //     width: 960,
        //     height: 589,
        //   },
        // ],
        dimensions: {
          width: 960,
          height: 589,
        },
      },
    },
  ]);

  mongoose.connection.close();
})();

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 4, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.11.1, 7.1.2 May 6, 2023
vkarpov15 added a commit that referenced this issue May 18, 2023
fix: timestamps on subdoc when not set on parent doc
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

Successfully merging a pull request may close this issue.

3 participants