Search by objectId doesn't work for MongoDB
See original GitHub issueIssue 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});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:26
Top Results From Across the Web
How do I search for an object by its ObjectId in the mongo ...
The query returned no results. I found the 4ecbe7f9e8c1c9092c000027 by doing db.theColl.find() and grabbing an ObjectId. There are several thousand objects in ...
Read more >ObjectId Atlas search doesn't work - MongoDB
Hi, I was trying to query an atlas search Index. The datatype is objectId and the index contains other paths which has autocomplete...
Read more >Search by ObjectId in mongodb with PyMongo - Python
Example 1: In this example, we have already stored the ObjectId object instance, then use the stored object instance to search for the ......
Read more >ObjectID() — MongoDB Node.JS Driver 1.4.9 documentation
If you find any issues with the documentation feel free to open a Jira Case and we'll work to resolve it promptly.
Read more >How does find by id Work in MongoDB | Examples - eduCBA
MongoDB provides a function with the name findById() which is used to retrieve the document matching the 'id' as specified by the user....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It doesn’t work, because
_id
is NOT instance of ObjectID. To make it work, you should manually createObjectID
instance, 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
id
to basically find by id in mongo. This is my entity class:It is definitely not a fix, more like a workaround.