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 fetch _id object from find method.

See original GitHub issue

I have created a some mongo documents via shell

db.users.insert([{ _id: 1, name: "angel" },
    { _id: 2, name: "justin" },
    { name: "rose" }]);

I have queried mongoose find to retrieve all users.

model.find(function (err, users) {
    if (err) {
        console.log(err);
    } else {
        console.log(users);
    }
});

The console shows as

[ { name: 'angel' },
  { name: 'justin' },
  { _id: 51d3c4a113cfcdb762000000, name: 'rose' } ]

It is missing _id in first two records as I have created _id for those records. The third one returns _id.

The schema for users

UserSchema = new Schema({
    name: String
}),

Why I am unable to fetch _id on manually created _id?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ebensingcommented, Jul 3, 2013

In this case, the ID that you create is a Number. By default, mongoose assumes that _id is an object ID. If you want to be able to get the manually create IDs, you need to specify that in your schema.

0reactions
justin-johncommented, Jan 11, 2017

Thank you Valeri.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can not find a record by it's _id in mongoDB
Your _id field isn't an ObjectId it is just a String. This should work: db.user.find({_id : "53a095aa4568cb1fef93f681"}).
Read more >
Nothing is returned on search of an existing document ...
With one or two collections to perform the same work, I am unable to find an existing record… The code that I am...
Read more >
Map.prototype.get() - JavaScript - MDN Web Docs - Mozilla
The get() method returns a specified element from a Map object. ... specified key, or undefined if the key can't be found in...
Read more >
Global Object Identification
This id should be a globally unique identifier for this object, and given just this id , the server should be able to...
Read more >
Not able to fetch the newly created child record id from ...
It is impossible for a newly-created parent record to have any child records until its own save process completes.
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