typeorm: Search by objectId doesn't work for MongoDB
Issue type:
[ ] question [ x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ x] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem:
import { ObjectID } from "mongodb";
import { Column, Entity, Index, ObjectIdColumn } from "typeorm";
@Entity()
@Index(["clientId", "name"], { unique: true })
export class User {
@ObjectIdColumn()
public id: ObjectID;
@Column()
public companyRef: ObjectID;
@Column()
public name: string;
@Column()
public mobile: number;
}
const repository = getMongoRepository(EventSchema);
const id = "hex-string";
// These work
repository.findOne(id);
repository.find({where:{ _id: id }});
// But this doesn't
repository.find({id: id, mobile: 021029022});
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 9
- Comments: 28
It doesn’t work, because
_idis NOT instance of ObjectID. To make it work, you should manually createObjectIDinstance, something like thisHey guys,
I was just reading the issue here and experimenting with some stuff and randomly I’ve nailed it:
Basically one have to pass only the
idto basically find by id in mongo. This is my entity class:It is definitely not a fix, more like a workaround.
Please read documents carefully it’s not an issue. You should not use
findin mongodb like that way. UsefindOneinstead.And if you want to use
findyou should usewhereinside of it.The only work around is:
then in your code to find a document by id you do:
For quick reference, I think this summarizes the issue:
For context:
I think this is a feature request and can be added! Simply if database type was mongo, change id => _id and the run the query
I can second this. This is very confusing. Being forced to use _id means that mongo-specific code is leaking into my business logic and even the front-end unless we do a bunch of manual serialization.
@pleerock is there a way to make this less confusing?
please reopen this issue…
The following is work for me, but when i use
import { ObjectID } from 'typeorm';, it is not work.Want to use id instead of _id. no more appropriate solution, please reopen this issue.
Please, reopen this issue. It makes no sense to use
_idwhen you explicitly decorate the ID attribute with@ObjectIdColumnThis issue is closed without a solution. My scenario:
Some more debugging info from another project: I’m using Typeorm version ^0.2.15
This works:
This fails with a duplicate ID error.
Here’s the error:
@tien So you saying that when you use
idcode doesn’t work but when you use_idit does?@deepchudasama sorry for the confusion, when I try
repository.findOne({id: id, mobile: 021029022});It also doesn’t work. But when I change the the entity definition fromIt work. Which is kind of confusing as the example usage of mongo use id instead of _id