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

find on ObjectIds not working after insertMany #14935

Closed
2 tasks done
DrChrispoper opened this issue Oct 4, 2024 · 2 comments · Fixed by #14938
Closed
2 tasks done

find on ObjectIds not working after insertMany #14935

DrChrispoper opened this issue Oct 4, 2024 · 2 comments · Fixed by #14938
Milestone

Comments

@DrChrispoper
Copy link

DrChrispoper commented Oct 4, 2024

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

@vkarpov15
Copy link
Collaborator

We have a fix in #14938 and will ship that fix in our next patch release.

As a workaround, you can:

  1. set flattenObjectIds to false in your schema
  2. use create() instead of insertMany()

@vkarpov15 vkarpov15 added this to the 8.7.1 milestone Oct 6, 2024
@DrChrispoper
Copy link
Author

Thanks for the help and the quick response. 🙏

vkarpov15 added a commit that referenced this issue Oct 9, 2024
fix: set flattenObjectIds to false when calling toObject() for internal purposes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants