No _id for single embedded sub documents
See original GitHub issueReproducible code:
const mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
const childSchema = new Schema({
name: String
});
const parentSchema = new Schema({
child: childSchema
});
const Parent = mongoose.model('Parent', parentSchema);
mongoose.connection.once('open', function() {
Parent.create({ child: { name: 'My child' }});
});
Result:
> db.parents.find()
{ "_id" : ObjectId("56eeed1417e215f41643ac8b"), "child" : { "name" : "My child" }, "__v" : 0 }
Expected behaviour is to see a an ObjectId for the child sub document as well. Using the array notation like this works:
const parentSchema = new Schema({
child: [childSchema]
});
> db.parents.find()
{ "_id" : ObjectId("56eeee3a0110d127174d318e"), "child" : [ { "name" : "My child", "_id" : ObjectId("56eeee3a0110d127174d318f") } ], "__v" : 0 }
Since support for single embedded sub documents has been introduced in 4.2 the first method should work as well unless I’m doing something wrong here.
mongoose version 4.4.4
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to only get the array nested subdocuments with that ...
how to get only the document that is related to that id and not the whole document? so far I have tried: db.contacts.find({"_id"...
Read more >MongoDB Embedded Objects have no ID (null value)
An ID identifies a root document, not sub-documents. There's no reason why you'd want to auto generate an id for a nested document...
Read more >Nested aka Sub-document entities with ID property not ...
Create another entity with an ID (ObjectId) property. Add a nested/sub-document property to the first entity referencing the second entity ...
Read more >Mongoose v6.8.1: SubDocuments
Subdocuments are documents embedded in other documents. In Mongoose, this means you can nest schemas in other schemas. Mongoose has two distinct notions...
Read more >MongoDB - Embedded Documents - GeeksforGeeks
Inside this document, we have a field named 'courseDetails'. This field contains a document and this document contains some field-value pairs ...
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 Free
Top 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
One man’s bug is another man’s feature 😃
Actually, this was an unintentional bug. Fixed in above, Use
{ _id: false }
in your schema options to avoid getting an _id.