Creación de Índices Multifamiliares en Mangosta / MongoDB
Estoy tratando de encontrar documentación, en vano, sobre cómo crear índices de múltiples campos en Mongoosejs. En particular, tengo dos campos que necesitan ser indexados y únicos. ¿Qué es un esquema de mangosta de ejemplo que indexa dos campos juntos?
72
3 answers
Llamas al método index
en tu objeto Schema
para hacer eso como se muestra aquí. Para su caso sería algo como:
mySchema.index({field1: 1, field2: 1}, {unique: true});
146
Author: JohnnyHK,
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
2012-09-24 22:53:13
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
2012-09-24 22:53:13
Es necesario definir índices a nivel de esquema al crear índices compuestos.
animalSchema.index({ name: 1, type: -1 });
Referencia: http://mongoosejs.com/docs/guide.html#indexes
7
Author: Krumb,
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-09-16 23:56:00
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-09-16 23:56:00
Following command can be used to create compound index for nested json:
db.ACCOUNT_collection.createIndex({"account.id":1,"account.customerId":1},{unique:1})
Mongo json structure is like :
{"_id":"648738"
"account": {
"id": "123",
"customerId": 7879,
"name": "test"
..
..
}
}
He probado con datos de muestra que funciona perfectamente como se esperaba.
0
Author: Rajeev Rathor,
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
2018-04-23 05:47:17
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
2018-04-23 05:47:17