typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

Mongo: problem renaming field _id (@ObjectIdColumn)

Open mrddter opened this issue 2 years ago • 3 comments

Issue description

Mongo: problem renaming field _id (@ObjectIdColumn)

Expected Behavior

Using an entity like this (where is used id field and not _id):

@Entity()
export class User {
   @ObjectIdColumn() // { name: '_id' }
   id: ObjectID

   @Index()
   @Column()
   email: string
}

I expected to load an entity in this way:

userRepository.preload({ id: id })
userRepository.findOne({ id: id })
userRepository.find({ where: { id: id } })
User.findOne({ id: id })
User.find({ where: { id: id } })

Actual Behavior

Actually, the find doesn't return anything even if on DB everything is correct. Other methods also fail, such as remove(entity) as evidenced by the example repo.

All works well for these finds:

User.find({ where: { email: '[email protected]' } })
User.findOneById(id)
userRepository.findOneById(id)

The create() / save() adds correctly a record like this:

{
"_id":{"$oid":"63caa1adde907b687dfa8a65"},
"email":"[email protected]"
}

But if you run the example (below) use see in the collection something like this:

{"_id":{"$oid":"63ce9be9d7fe09a6b790d9de"},"email":"[email protected]"}

{"_id":{"$oid":"63ce9be9d7fe09a6b790d9df"},"email":"[email protected]","id":null}

{"_id":{"$oid":"63ce9bead7fe09a6b790d9e0"},"email":"[email protected]","id":{"$oid":"63ce9be9d7fe09a6b790d9df"}}

{"_id":{"$oid":"63ce9bead7fe09a6b790d9e1"},"email":"[email protected]","id":null}

Console output:

User has been saved. Lets insert a new one to update it later
Second entity has been inserted. Lets update it
Entity has been updated. Persist once again to make find and remove then
Entity is loaded:  User { email: '[email protected]' }
Now remove it by entity {"email":"[email protected]"}
Cannot save. Error:  SubjectWithoutIdentifierError: Internal error. Subject User must have an identifier to perform operation.
    at /Users/dev/typeorm-mongo/node_modules/src/persistence/SubjectExecutor.ts:682:27
    at Array.map (<anonymous>)
    at SubjectExecutor.executeRemoveOperations (/Users/dev/typeorm-mongo/node_modules/src/persistence/SubjectExecutor.ts:680:41)
    at SubjectExecutor.execute (/Users/dev/typeorm-mongo/node_modules/src/persistence/SubjectExecutor.ts:156:20)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async EntityPersistExecutor.execute (/Users/dev/typeorm-mongo/node_modules/src/persistence/EntityPersistExecutor.ts:197:21) SubjectWithoutIdentifierError: Internal error. Subject User must have an identifier to perform operation.
    at /Users/dev/typeorm-mongo/node_modules/src/persistence/SubjectExecutor.ts:682:27
    at Array.map (<anonymous>)
    at SubjectExecutor.executeRemoveOperations (/Users/dev/typeorm-mongo/node_modules/src/persistence/SubjectExecutor.ts:680:41)
    at SubjectExecutor.execute (/Users/dev/typeorm-mongo/node_modules/src/persistence/SubjectExecutor.ts:156:20)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async EntityPersistExecutor.execute (/Users/dev/typeorm-mongo/node_modules/src/persistence/EntityPersistExecutor.ts:197:21)

Steps to reproduce

You can use this repo

https://github.com/mrddter/typeorm-mongo.git

Thanks

My Environment

Dependency Version
Operating System macOS 13.1
Node.js version 18.12.0
Typescript version 4.9.4
TypeORM version 0.3.11

Additional Context

No response

Relevant Database Driver(s)

  • [ ] aurora-mysql
  • [ ] aurora-postgres
  • [ ] better-sqlite3
  • [ ] cockroachdb
  • [ ] cordova
  • [ ] expo
  • [X] mongodb
  • [ ] mysql
  • [ ] nativescript
  • [ ] oracle
  • [ ] postgres
  • [ ] react-native
  • [ ] sap
  • [ ] spanner
  • [ ] sqlite
  • [ ] sqlite-abstract
  • [ ] sqljs
  • [ ] sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don't know how to start. I would need guidance.

mrddter avatar Jan 23 '23 14:01 mrddter