question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Search by objectId doesn't work for MongoDB

See original GitHub issue

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});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:26

github_iconTop GitHub Comments

22reactions
belukhacommented, Feb 24, 2020

It doesn’t work, because _id is NOT instance of ObjectID. To make it work, you should manually create ObjectID instance, something like this

//Note, that I use constructor from mongodb and type from typeorm, but they shouldn't have the different names if they're in different files
import { ObjectID } from 'mongodb';
import { ObjectID as ObjectIDType} from 'typeorm'

@Entity()
export class User {
  @ObjectIdColumn()
  _id: ObjectIDType;

  @Column()
  name: string;
}

const userId = '5d961d865ede2a35d0f51207';

const userRepository = connection.getMongoRepository(User);
const user = await userRepository.findOne({
 _id: new ObjectID(userId)
});

//or just use
const user = await userRepository.findOne(userId)
18reactions
viktorstaikovcommented, Nov 14, 2019

Hey guys,

I was just reading the issue here and experimenting with some stuff and randomly I’ve nailed it:

async findById(id: string): Promise<UserEntity> {
   return await this.userRepository.findOne(id);
}

Basically one have to pass only the id to basically find by id in mongo. This is my entity class:

@Entity('users')
export class UserEntity {
  @ObjectIdColumn()
  id: string;

  @Column({ unique: true })
  email: string;

  @Column()
  password: string;
}

It is definitely not a fix, more like a workaround.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found