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

Timestamps are not being created when using $setOnInsert #10460

Closed
lbertoncello opened this issue Jul 16, 2021 · 2 comments
Closed

Timestamps are not being created when using $setOnInsert #10460

lbertoncello opened this issue Jul 16, 2021 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@lbertoncello
Copy link

lbertoncello commented Jul 16, 2021

Maybe I've found a new bug related to timestamps, like this one I've described recently.

Timestamps are not being created when I use $setOnInsert in findOneAndUpdate.

You can see here the code I've used to test it:

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

const userSchema = new Schema({
	email: {
		type: String,
		required: true,
	},
	fullname: {
		type: String,
		required: true,
	},
	dateOfBirth: {
		type: Date,
		required: true,
	},
	contacted: {
		type: Boolean,
		required: true,
		default: false,
	},
}, {
	timestamps: true,
});

const eventSchema = new Schema({
	code: {
		type: String,
		required: true,
	},
	template: String,
	users: {
		type: [ userSchema ],
		required: false,
	},
	message: String,
}, {
	timestamps: true,
});


const churchSchema = new Schema({
	churchId: {
		type: Number,
		required: true,
	},
	integration: String,
	service: String,
	events: {
		type: [ eventSchema ],
		required: false,
	},
}, {
	timestamps: true,
});

const Test = mongoose.model('Church', churchSchema);

async function run () {
	await mongoose.connect('mongodb://localhost:27017/test', {
		useNewUrlParser: true,
		useUnifiedTopology: true,
	});
	await mongoose.connection.dropDatabase();

	const query = { churchId: 1 };
	const update = {
		$setOnInsert: {
			churchId: 1,
			integration: 'Hello',
			service: 'world',
			events: [
				{
					code: 'A',
					template: 'B',
					users: [
						{
							email: 'Test@testerson.com',
							fullname: 'Fullname',
							dateOfBirth: new Date(),
							contacted: true,
						},
					],
					message: 'message',
				},
			],
		},
	};
	const options = {
		upsert: true,
		new: true,
		timestamps: true,
	};

	const entry = await Test.findOneAndUpdate(query, update, options);

	const result = await Test.findOneAndUpdate({
		_id: entry._id,
		events: { $elemMatch: { code: 'A' } },
	}, {
		$addToSet: {
			'events.$.users': {
				email: 'randal@test.com',
				fullname: 'fred',
				dateOfBirth: new Date(),
				contacted: true,
			},
		},
	}, {
		upsert: true,
		new: true,
		timestamps: true,
	});

	console.log(result);
	console.log(result.events[0].users);
}

run();

I've got the following output:

{
  _id: 60f1d8db995dbbbe6da2aa4a,
  churchId: 1,
  __v: 0,
  createdAt: 2021-07-16T19:07:07.281Z,
  events: [
    {
      _id: 60f1d8db6b5880242c18fce6,
      code: 'A',
      template: 'B',
      users: [Array],
      message: 'message'
    }
  ],
  integration: 'Hello',
  service: 'world',
  updatedAt: 2021-07-16T19:07:07.303Z
}
[
  {
    contacted: true,
    _id: 60f1d8db6b5880242c18fce7,
    email: 'Test@testerson.com',
    fullname: 'Fullname',
    dateOfBirth: 2021-07-16T19:07:07.279Z
  },
  {
    contacted: true,
    _id: 60f1d8db6b5880242c18fce9,
    email: 'randal@test.com',
    fullname: 'fred',
    dateOfBirth: 2021-07-16T19:07:07.303Z,
    updatedAt: 2021-07-16T19:07:07.303Z,
    createdAt: 2021-07-16T19:07:07.303Z
  }
]

Is this a bug or an expected behaviour?


MongoDB: 4.4.6
Mongoose: 5.13.3
Node.js: v14.17.0

@vkarpov15 vkarpov15 added this to the 5.13.4 milestone Jul 19, 2021
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jul 19, 2021
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jul 28, 2021
@vkarpov15
Copy link
Collaborator

This is a bug. The fix will be in v5.13.4 👍

@lbertoncello
Copy link
Author

This is a bug. The fix will be in v5.13.4 👍

Ok, thanks!

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