Unable to fetch _id object from find method.
See original GitHub issueI 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:
- Created 10 years ago
- Comments:11 (2 by maintainers)
Top 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 >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
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.
Thank you Valeri.