-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Closed
Milestone
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.7.0
Node.js version
18.x
MongoDB server version
7.0.12
Typescript version (if applicable)
^5.1.6
Description
After insertMany calling find with on an ObjectId parameter, it will fail to return any rows.
The issue happens when I have both a property of type: [Schema.Types.Mixed] and toObject: { flattenObjectIds: true }
I'm not sure why, but I've done written a jest test to show.
Steps to Reproduce
import db from '../helper/mockDb'
import mongoose, { Schema, Types } from 'mongoose'
const userId = new Types.ObjectId()
beforeAll(async () => {
await db.connect()
})
afterAll(async () => {
await db.disconnect()
})
describe('Basic insertMany test', () => {
const TestSchema = new Schema(
{
professionalId: {
type: Schema.Types.ObjectId,
},
firstName: {
type: String,
},
},
{
toObject: { flattenObjectIds: true },
},
)
const TestModel = mongoose.model('testSchema', TestSchema)
const SimpleExternalClientSchema = new Schema(
{
professionalId: {
type: Schema.Types.ObjectId,
},
firstName: {
type: String,
default: '',
required: true,
trim: true,
},
addresses: {
type: [Schema.Types.Mixed],
default: [],
},
},
{
toObject: { flattenObjectIds: true },
},
)
const SimpleExternalClientModel = mongoose.model('simpleExternalClient', SimpleExternalClientSchema)
test('Should insert multiple records', async () => {
const data = [
{ professionalId: userId, firstName: 'Testy' },
{ professionalId: userId, firstName: 'Johny' },
]
await TestModel.insertMany(data)
const records = await TestModel.find({}).exec()
expect(records.length).toEqual(2)
const recordFiltered = await TestModel.find({ professionalId: userId }).exec()
expect(recordFiltered.length).toEqual(2) // <- This works
})
test('Should insert client multiple records', async () => {
const externalClientData = [{
firstName: 'Testy',
professionalId: userId,
}, {
firstName: 'Testy',
professionalId: userId,
}]
await SimpleExternalClientModel.insertMany(externalClientData)
const records = await SimpleExternalClientModel.find({}).exec()
expect(records.length).toEqual(2)
const recordFiltered = await SimpleExternalClientModel.find({ professionalId: userId }).exec()
expect(recordFiltered.length).toEqual(2) // <-- This is the line that is failing
})
})
Expected Behavior
Second test should pass
Metadata
Metadata
Assignees
Labels
No labels