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.

No _id for single embedded sub documents

See original GitHub issue

Reproducible 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:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
vkarpov15commented, Apr 4, 2016

One man’s bug is another man’s feature 😃

1reaction
vkarpov15commented, Apr 2, 2016

Actually, this was an unintentional bug. Fixed in above, Use { _id: false } in your schema options to avoid getting an _id.

Read more comments on GitHub >

github_iconTop 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 >

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