Atlas
Atlas

Reputation: 3513

Stop Mongoose from creating _id property for sub-document array items

If you have subdocument arrays, Mongoose automatically creates ids for each one. Example:

{
    _id: "mainId"
    subDocArray: [
      {
        _id: "unwantedId",
        field: "value"
      },
      {
        _id: "unwantedId",
        field: "value"
      }
    ]
}

Is there a way to tell Mongoose to not create ids for objects within an array?

Upvotes: 341

Views: 148376

Answers (8)

Tushar Dhingra
Tushar Dhingra

Reputation: 21

TypeError: Invalid schema configuration: false is not a valid type at path id.

_id: { id: false },

If adding this line makes your app show the error message mentioned above, you might be using the newest mongoose 7 module versions.

To solve it, modify the mentioned line by adding an underscore _ before the id in the { _id: false } syntax on that line. So, that line should now look like this:

_id: { _id: false },

Upvotes: 2

wongx
wongx

Reputation: 12147

NestJS example for anyone looking for a solution with decorators

@Schema({_id: false})
export class MySubDocument {
    @Prop()
    id: string;
}

Below is some additional information from the Mongoose Schema Type definitions for id and _id:

/**
* Mongoose assigns each of your schemas an id virtual getter by default which returns the document's _id field
* cast to a string, or in the case of ObjectIds, its hexString.
*/
id?: boolean;

/**
* Mongoose assigns each of your schemas an _id field by default if one is not passed into the Schema
* constructor. The type assigned is an ObjectId to coincide with MongoDB's default behavior. If you
* don't want an _id added to your schema at all, you may disable it using this option.
*/
_id?: boolean;

Upvotes: 6

Joel Grenon
Joel Grenon

Reputation: 3273

You can create sub-documents without schema and avoid _id. Just add _id: false to your subdocument declaration.

var schema = new mongoose.Schema({
   field1: {
      type: String
   },
   subdocArray: [{
      _id: false,
      field: { type: String }
   }]
});

This will prevent the creation of an _id field in your subdoc.

Tested in Mongoose v5.9.10

Upvotes: 218

throrin19
throrin19

Reputation: 18207

It's simple, you can define this in the subschema :

var mongoose = require("mongoose");

var subSchema = mongoose.Schema({
    // your subschema content
}, { _id : false });

var schema = mongoose.Schema({
    // schema content
    subSchemaCollection : [subSchema]
});

var model = mongoose.model('tablename', schema);

Upvotes: 454

Oliver White
Oliver White

Reputation: 99

If you want to use a predefined schema (with _id) as subdocument (without _id), you can do as follow in theory :

const sourceSchema = mongoose.Schema({
    key : value
})
const subSourceSchema = sourceSchema.clone().set('_id',false);

But that didn't work for me. So I added that :

delete subSourceSchema.paths._id;

Now I can include subSourceSchema in my parent document without _id. I'm not sure this is the clean way to do it, but it work.

Upvotes: 4

Deeksha Sharma
Deeksha Sharma

Reputation: 3359

You can use either of the one

var subSchema = mongoose.Schema({
//subschema fields    

},{ _id : false });

or

var subSchema = mongoose.Schema({
//subschema content
_id : false    

});

Check your mongoose version before using the second option

Upvotes: 8

jemiloii
jemiloii

Reputation: 25759

I'm using mongoose 4.6.3 and all I had to do was add _id: false in the schema, no need to make a subschema.

{
    _id: ObjectId
    subDocArray: [
      {
        _id: false,
        field: "String"
      }
    ]
}

Upvotes: 34

wlingke
wlingke

Reputation: 4799

Additionally, if you use an object literal syntax for specifying a sub-schema, you may also just add _id: false to supress it.

{
   sub: {
      property1: String,
      property2: String,
      _id: false
   }
}

Upvotes: 63

Related Questions