-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
populate() incorrectly populates virtual fields in an embedded subdocument array, when using sort options, depending on the number of matches #14018
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
const mongoose = require('mongoose');
const shiftSchema = new mongoose.Schema({
employeeId: mongoose.Types.ObjectId,
startedAt: Date,
endedAt: Date
});
const Shift = mongoose.model('Shift', shiftSchema);
const employeeSchema = new mongoose.Schema({
name: String,
});
employeeSchema.virtual('mostRecentShift', {
ref: Shift,
localField: '_id',
foreignField: 'employeeId',
options: {
sort: { startedAt: -1 },
},
justOne: true,
});
const storeSchema = new mongoose.Schema({
location: String,
employees: [employeeSchema],
});
const Store = mongoose.model('Store', storeSchema)
async function run() {
console.log(mongoose.version);
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const store = await Store.create({
location: 'Tashbaan',
employees: [
{ name: 'Aravis' },
{ name: 'Shasta' },
],
});
const employeeAravis = store.employees.find(({ name }) => name === 'Aravis');
const employeeShasta = store.employees.find(({ name }) => name === 'Shasta');
await Shift.insertMany([
{ employeeId: employeeAravis._id, startedAt: new Date(Date.now() - 57600000), endedAt: new Date(Date.now() - 43200000) },
{ employeeId: employeeAravis._id, startedAt: new Date(Date.now() - 28800000), endedAt: new Date(Date.now() - 14400000) },
{ employeeId: employeeShasta._id, startedAt: new Date(Date.now() - 14400000), endedAt: new Date() },
])
const storeWithMostRecentShifts = await Store.findOne({ location: 'Tashbaan' })
.populate('employees.mostRecentShift')
.select('-__v')
.exec();
// When Aravis has two shifts, mostRecentShift is populated correctly for both employees
console.log('When Aravis has 2 shifts and Shasta has 1 shift')
console.log(JSON.stringify(storeWithMostRecentShifts.toJSON({ virtuals: true }).employees, null, 4));
// Now I remove Aravis's oldest shift, so that each employee has exactly one shift
await Shift.findOne({ employeeId: employeeAravis._id }).sort({ startedAt: 1 }).then((s) => s.deleteOne());
// Re-populating mostRecentShift
const storeWithMostRecentShiftsNew = await Store.findOne({ location: 'Tashbaan' })
.populate('employees.mostRecentShift')
.select('-__v')
.exec();
// Now, mostRecentShift is populated incorrectly, with Aravis's shift under Shasta's mostRecentShift, and vice-versa
console.log('When both employees have 1 shift each')
console.log(JSON.stringify(storeWithMostRecentShiftsNew.toJSON({ virtuals: true }).employees, null, 4));
}
run(); |
I want to work on this task. |
vkarpov15
added a commit
that referenced
this issue
Nov 21, 2023
…rtual underneath doc array with justOne Fix #14018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
7.6.3
Node.js version
18.16.1
MongoDB server version
6.0.11
Typescript version (if applicable)
No response
Description
I have a model
Store
with an array propertyemployees
where each element is a nested schemaEmployeeSchema
. I have another modelShift
which contains details of the shifts for every employee. The schemaEmployeeSchema
has a virtual calledmostRecentShift
withShift
as a reference.mostRecentShift
uses optionjustOne: true
and sort optionstartedAt: -1
.When calling
store.populate('employees.mostRecentShift')
, when there is exactly one shift for each employee,mostRecentShift
is incorrectly populated with the other employee's shift.Steps to Reproduce
The following code shows the behavior of populate in two cases:
Output:
(Note how employee's
id
does not matchmostRecentShift.employeeId
when there is exactly one shift for each)Expected Behavior
mostRecentShift
should have been populated correctly when each employee had exactly one shift each, similar to when Aravis had 2 shifts.Expected output:
The text was updated successfully, but these errors were encountered: