Problem with mongodb operation occasionally not updating with findOneAndUpdate nested arrays

Hi there!

I have an application which needs to do updates to some nested arrays. Following the mongoDB documentation, I have the following query:

	const updatedDinner = await Dinner.findOneAndUpdate(
			{ 'cups.plates': { $elemMatch: { id: plateId } } },
			{
				$set: {
					updatedBy: 'me',
					'cups.$[].plates.$[plate].status': 'COMPLETE',
					'cups.$[].plates.$[plate].outputs':
						finishedOutputs.map(formatOutput),
				},
			},
			{
				arrayFilters: [{ 'plate.id': plateId }],
				new: true,
			}
		).exec();

This works MOST of the time, but every tenth or so time I don’t see the changes reflected in the database. The logs all look normal as if the update has gone through, but I just can’t see the change reflected in the database. any guidance would be much appreciated! Many thanks.

This is the data model:

const dinnerSchema = new mongoose.Schema(
	{
		id: {
			type: String,
			default: uuidV4,
			index: true,
		},
		updatedBy: String,
		cups: [
			{
				plates: [
					{
						id: {
							type: Number,
							index: true,
						},
						status: String,
						outputs: [],
					},
				],
			},
		],
	},

	{
		timestamps: true,
	}
);

I do not really know mongoose so my comment might be off.

The first thing that could help you is to handle errors and exceptions. From the little code snippet you share it seems like you are silently ignoring them.

Next you could share sample documents and the values of your variables like plateId.