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

Deeply nested document doesn't get timestamps created/updated #11917

Closed
2 tasks done
kravorkid opened this issue Jun 9, 2022 · 1 comment
Closed
2 tasks done

Deeply nested document doesn't get timestamps created/updated #11917

kravorkid opened this issue Jun 9, 2022 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@kravorkid
Copy link

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.3.6

Node.js version

16.14.x

MongoDB server version

5.x

Description

Hi, i found this issue that could be related,
but in my use case i've tried to make a deeply nested array of subdocument with timestamps but it looks like it doesn't work.
There is no mention of such a limit in the doc, if you can enlighten me on that i would be glad !

Steps to Reproduce

import mongoose from 'mongoose'
const Schema = mongoose.Schema

const nestedSettingsSchema = new Schema({
  value: String,
  active: Boolean
},
{
  timestamps: true,
  _id: false
})

const userSettignsSchema = new Schema({

  nested_settings: {
    settings_prop: [nestedSettingsSchema]
  },
  other_settings_props: String
}, { _id: false })

const userSchema = new Schema({
  first_name: String,
  last_name: String,
  settings: userSettignsSchema
}, {
  timestamps: true
})

const UserTest = mongoose.model('UserTest', userSchema)

const addUser = async () => await UserTest.create({
  first_name: 'jon',
  last_name: 'doe',
  settings: { nested_settings: { settings_prop: [{ value: 'stuff', active: true }] } },
  other_settings_props: 'config'
})

addUser()

Expected Behavior

settings_prop should have default timestamps value

@kravorkid kravorkid changed the title Deeply nested document does get timestamps updated Deeply nested document doesn't get timestamps created/updated Jun 9, 2022
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jun 9, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
const Schema = mongoose.Schema

const nestedSettingsSchema = new Schema({
  value: String,
  active: Boolean
},
{
  timestamps: true,
  _id: false
})

const userSettignsSchema = new Schema({

  nested_settings: {
    settings_prop: [nestedSettingsSchema]
  },
  other_settings_props: String
}, { _id: false })

const userSchema = new Schema({
  first_name: String,
  last_name: String,
  settings: userSettignsSchema
}, {
  timestamps: true
})

const UserTest = mongoose.model('UserTest', userSchema)

async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();
    const entry = await UserTest.create({
        first_name: 'jon',
        last_name: 'doe',
        settings: { nested_settings: { settings_prop: [{ value: 'stuff', active: true }] } },
        other_settings_props: 'config'
    });
    console.log(entry.settings.nested_settings.settings_prop);

}
run();

@vkarpov15 vkarpov15 added this to the 6.3.8 milestone Jun 13, 2022
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

3 participants