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.

Unable to use mongo _id for await User.find(_id).

See original GitHub issue

Able to find user with _id (objectID2) and user generated _id (abc2) but not working with auto-generated _id (“607cf59ec294c8c3f1261e4f”)

await User.find(_id as any)  // works with _id as objectId
User { 
  _id: ObjectId2 {
    [Symbol(id)]: Buffer2(12) [       
       96, 124, 245, 158, 194,        
      148, 200, 195, 241,  38,        
       30,  79
    ]
  },
  name: "user1"
}

await User.find("607cf59ec294c8c3f1261e4f")  // undefined

await User.find("abc1") // works

Auto-generated _id is 607cf59ec294c8c3f1261e4f image

Auto-generated _id returns undefined image

User generated _id works image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
borsemayur2commented, Apr 22, 2021

Hey Mayur,

Thank you for sharing all this. I’m sure this can easily be fixed 😃

I’ve looked at the mongo connector and basically whenever you do .find(...) this is what it does:

await User.find(id);
// =>
await User.where({ _id: id }).get();

If we make an update, delete something, the id problem would be similar:

await User.where({ _id: id }).update({ name: "Mayur" });

From my perspective, it then seems that the where clause is the issue every time.

What we could do is to check whenever the connector has to deal with wheres, it could:

* Check for the field name

* If it equals to `_id`, then wrap it with an object id

* Otherwise do nothing (we keep the field name)

I could give it a try but if you’re adventurous:

* Look for `.wheres` and `.whereIn` into the [mongo connector](https://github.com/eveningkid/denodb/blob/master/lib/connectors/mongodb-connector.ts)

* [This is how `.wheres` are added to a query](https://github.com/eveningkid/denodb/blob/8dfb7ad30862b9d5a9bed967cda6c48a0101946a/lib/query-builder.ts#L177)

* [This is how `.whereIn` are added to a query](https://github.com/eveningkid/denodb/blob/8dfb7ad30862b9d5a9bed967cda6c48a0101946a/lib/query-builder.ts#L142)

The goal is to only update the mongo connector, not Model that should remain generic.

Let me know if you want to give it a try 😃

Tried this in #240

1reaction
borsemayur2commented, Apr 21, 2021

Sure. I’ll try it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't get _id mongodb - node.js - Stack Overflow
findById is not taking an object, you just need to pass the id directly to findById const course = await Course.findById(id).lean().
Read more >
Mongoose v6.8.1: API docs
The Mongoose ObjectId SchemaType. Used for declaring paths in your schema that should be MongoDB ObjectIds. Do not use this to create a...
Read more >
Can't update document with object from Stripe - MongoDB
I've been updating the stripe checkout and know need to append the object to the user document each time there's a purchase. I've...
Read more >
Mongoose findById method not properly casting to id strings to ...
Running db.users.findOne({ _id: "<id-string>" }) in the mongo shell returns the proper user document. Using the findOne ...
Read more >
mongodb cast to objectid failed for value - You.com - You.com
One way to resolve this is to add a check prior to your findById call to see if id is a valid ObjectId...
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