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.

If you explicitly declare _id: Schema.ObjectId for your model, then the ObjectId will not be available after new or save.

See original GitHub issue

If you explicitly declare

_id: Schema.ObjectId

for your model, then the ObjectId will not be available after new or save.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:6

github_iconTop GitHub Comments

20reactions
aheckmanncommented, Feb 12, 2013

ah, the docs were incorrectly hiding the option you need (will be fixed next time they’re published).

you must pass the auto option like so:

new Schema({ _id: { type: Schema.ObjectId, auto: true }})
0reactions
jeshuneocommented, Dec 3, 2019

new Schema({ _id: { type: Schema.ObjectId, auto: true }}) “CHANGE (_id) to (id)”

Read more comments on GitHub >

github_iconTop Results From Across the Web

is there a way to auto generate ObjectId when a mongoose ...
If you create a new document without an _id field, MongoDB automatically creates the field and assigns a unique BSON ObjectId. Share.
Read more >
Mongoose v6.8.2: Schemas
Mongoose assigns each of your schemas an _id field by default if one is not passed into the Schema constructor. The type assigned...
Read more >
Express Tutorial Part 3: Using a Database (with Mongoose)
It explains how object schema and models are declared, the main field types, and basic validation. It also briefly shows a few of...
Read more >
Mongoose | Ts.ED - A Node.js and TypeScript Framework on ...
This tutorial shows you how you can use mongoose package with Ts.ED. ... import {Property} from "@tsed/schema"; import {Model, ObjectID} ...
Read more >
MongoDB Schema using Mongoose - Arun Rajeevan - Medium
Note: When there's no document, parent.author will be null. If you have an array of authors in your parent Schema , populate() will...
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