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

findOneAndReplace does not set timestamps #9951

Closed
aaronadamsCA opened this issue Feb 18, 2021 · 1 comment
Closed

findOneAndReplace does not set timestamps #9951

aaronadamsCA opened this issue Feb 18, 2021 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@aaronadamsCA
Copy link

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

Report a bug.

What is the current behavior?

I am calling Model.findOneAndReplace() on a model with timestamps enabled. The resulting documents have no timestamps. Adding timestamps: true has no effect.

Calling Model.findOneAndUpdate() on the same model with overwrite: true results in correct timestamps, leading me to believe this issue is specific to Model.findOneAndReplace().

If the current behavior is a bug, please provide the steps to reproduce.

const product = await Product.findOneAndReplace(
  { externalId },
  { externalId, ...rest },
  { upsert: true, setDefaultsOnInsert: true, new: true }
).exec();

I can't currently provide a standalone PR but I still wanted to get this report in, in case it's something obvious.

What is the expected behavior?

Insert or overwrite with timestamps, as this similar call does:

const product = await Product.findOneAndUpdate(
  { externalId },
  { externalId, ...rest },
  { overwrite: true, upsert: true, setDefaultsOnInsert: true, new: true }
);

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js 14.15.4, Mongoose 5.11.17, MongoDB 3.6.4 (MongoDB Atlas 4.4.3).

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Feb 22, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Feb 22, 2021

const mongoose = require('mongoose');

mongoose.connect("mongodb://localhost:27017/", { useNewUrlParser: true, useUnifiedTopology: true });
const data = mongoose.connection;
data.on("open", function () {
    data.db.dropDatabase(console.log('database drop'));
});

const userSchema = new mongoose.Schema({
    email: {
      type: String,
      unique: true // `email` must be unique
    }
  },{timestamps: true});
  const User = mongoose.model('User', userSchema);
  async function test() {
    console.log(await User.create({email: 'test@google.com'}));
    console.log(await User.findOneAndReplace({email:'test@google.com'},{email: 'test2@google.com'}, {new:true, overwrite: true, upsert: true, rawResult:true}));
  }

  test();

@vkarpov15 vkarpov15 added this to the 5.11.19 milestone Feb 23, 2021
vkarpov15 added a commit that referenced this issue Mar 1, 2021
This was referenced Mar 5, 2021
This was referenced Mar 12, 2021
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