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

I'm experiencing a similar issue with child of child #11775

Closed
Axselll opened this issue May 5, 2022 · 4 comments
Closed

I'm experiencing a similar issue with child of child #11775

Axselll opened this issue May 5, 2022 · 4 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Axselll
Copy link

Axselll commented May 5, 2022

I'm experiencing a similar issue with child of child

const JournalSchema = new mongoose.Schema({
  message: {type: String},
}, {timestamps:true})

const ProductSchema = new mongoose.Schema({
  name: {type: String},
  journal: [JournalSchema]
}, {timestamps:true})

var LoTSchema = new mongoose.Schema({
  products: [ProductSchema],
}, {timestamps:true})

const LotsModel = mongoose.model('LoTs', LoTSchema)

const payload = {
  name: 'mock-name',
  journal: [{message: 'mock-message}]
}

  function addProductIntLot (id, payload) {
    const conditions = { _id: id }
    const update = { $push: { products: payload } }
    const options = { new: true, runValidators: true, fields: { products: { $slice: -1 } } }

    return LotsModel.model.findOneAndUpdate(conditions, update, options)
  }


journal entry never gets createdAt & updatedAt fields.

_Originally posted by @AlbertHambardzumyan in https://github.com/Automattic/mongoose/issues/4049#issuecomment-620494418_
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 9, 2022
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented May 9, 2022

result.products[1].journal does not contain timestamps

const { default: mongoose } = require("mongoose")

const JournalSchema = new mongoose.Schema({
    message: {type: String},
  }, {timestamps:true})
  
  const ProductSchema = new mongoose.Schema({
    name: {type: String},
    journal: [JournalSchema]
  }, {timestamps:true})
  
  var LoTSchema = new mongoose.Schema({
    products: [ProductSchema],
  }, {timestamps:true})
  
  const LotsModel = mongoose.model('LoTs', LoTSchema)
  
  const payload = {
    name: 'mock-name',
    journal: [{message: 'mock-message'}]
  }
  
  async function run() {

    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();
   const condition = await LotsModel.create({
        products: [{name: 'Test', journal: [{message: 'Testerson'}]}]
    });
    const update = { $push: { products: payload } }
    const options = { new: true, runValidators: true, fields: { products: { $slice: -1 } } }

    await LotsModel.findOneAndUpdate({_id: condition._id}, update, options);
    const result = await LotsModel.findOne();
    console.log(result.products[0]);
    console.log(result.products[1]);
console.log(result.products[1].journal);
  }

  run();

@Axselll
Copy link
Author

Axselll commented May 15, 2022

i solved it by adding createdAt and updatedAt manually. when i update my data i pass createdAt: false and updatedAt: date.now(), but i still wondering why timestamps: true won't generate those field if i use subdocument

@vkarpov15 vkarpov15 added this to the 6.3.6 milestone Jun 5, 2022
@Axselll
Copy link
Author

Axselll commented Jun 17, 2022

@vkarpov15 hey man, thanks for the great work, you are awesome👍

@vkarpov15
Copy link
Collaborator

image

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