Impedir que Mangosta cree la propiedad id para elementos de matriz de sub-documentos


Si tiene matrices de subdocumentos, Mongoose crea automáticamente ID para cada una. Ejemplo:

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

¿Hay alguna forma de decirle a Mongoose que no cree id para objetos dentro de un array?

Author: Talha Awan, 2013-06-22

5 answers

Es simple, puede definir esto en el subsquema:

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);
 194
Author: throrin19,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-09 11:52:07

Puede crear sub-documentos sin esquema y evitar _id. Simplemente agregue _id: false a su declaración de subdocumento.

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

Esto evitará la creación de un campo _id en tu subdoc. Probado en Mangosta 3.8.1

 90
Author: Joel Grenon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-03 09:01:00

Además, si usa una sintaxis literal de objeto para especificar un sub esquema, también puede agregar _id: false para suprimirlo.

{
   sub: {
      property1: String,
      property2: String,
      _id: false
   }
}
 30
Author: wlingke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-28 05:12:41

Estoy usando mongoose 4.6.3 y todo lo que tenía que hacer era agregar _id: false en el esquema, sin necesidad de hacer un subschema.

{
    _id: ObjectId
    subdocArray: [
      {
        _id: false,
        field: "String"
      }
    ]
}
 13
Author: jemiloii,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-14 23:08:26

En mongoose v.3 ahora tiene una forma alternativa de crear sub-documentos sin relaciones padres - hijos. Y estos sub-docs no tendrán índice

var mongoose = require("mongoose");

var schema = mongoose.Schema({
  // schema content
subSchema: [{
    firstname: 'sub name',
    lastname: 'last name'
  }]
});

var model = mongoose.model('tablename', schema);
 -3
Author: khex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-24 11:00:59