Mongoose: Fehler bei Indizes (die Indizes werden nicht erstellt)

Erstellt am 1. Juni 2017  ·  3Kommentare  ·  Quelle: Automattic/mongoose

HI Ich habe ein Modell, das verwendet wird, um Likes für ein "Nachrichten" -Modell wie dieses zu speichern:

import mongoose, { Schema } from 'mongoose';
mongoose.set('debug', true);

var newsLikesSchema = new Schema({
  _news_id: { type: Schema.Types.ObjectId, ref: 'news' },
  condominiums_id: { type: Number, required: true },
  users_id: { type: Number, required: true },
  created_at: { type: Date, default: Date.now },
}, {
  emitIndexErrors: true
});

newsLikesSchema.on('error', function(errorE) {
  console.log('---> index error: ', errorE);
});

newsLikesSchema.on('index', function(errI) {
  console.log('----> new index creating', errI);
});

// Index
newsLikesSchema.index({ _news_id: 1, users_id: 1 }, {unique: true}); // unique like per news/nuser
newsLikesSchema.index({ _news_id: 1, created_at: 1 }); // to sort by news_id and date
newsLikesSchema.index({ users_id: 1 });

var newsLikesM = mongoose.model('newslikes', newsLikesSchema);

module.exports = newsLikesM;

Das Problem ist, dass die Indizes nicht erstellt werden und ich kein Feedback von den Callbacks ".on('error')" und ".on('index')" habe , sie werden nicht ausgeführt.

MongoDB-Version: 3.4.2
Mungo-Version: 4.10.4
NodeJS-Version: v6.7.0

Hilfreichster Kommentar

Ich setze autoIndex:false und rufe Model.ensureIndexes() auf, um den Index zu erstellen, aber den Index nicht zu erstellen. Hängt das mit dem Problem zusammen?

'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var uniqueValidator = require('mongoose-unique-validator');

mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017/gh3396',{ config: {autoIndex: false} });

// Variables
var ClientSchema = new Schema(
    {
      name : {
        type     : String,
        required : true,
        trim     : true
      },
      organization : {
        type     : String,
        required : true
      }
    }
  )

// Indexes
ClientSchema.index({ "name" : 1, "organization" : 1 }, { unique : true })

// Plugins
ClientSchema.plugin(uniqueValidator, {
  message : 'Name must be unique.'
})

const Client = mongoose.model('Client', ClientSchema);

Client.ensureIndexes()
Client.on('index', function(err){
console.log(err)
})

new Client({
  name: 'a',
  organization:'a'
}).save().then((result) => {
  console.log(result)
}, (err) => {
  console.log(err)
})

und es erstellt einfach den _id-Index als Standard:

> use gh3396
switched to db gh3396
> show collections
clients
> db.clients.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "gh3396.clients"
    }
]

Irgendwelche Ideen?
MongoDB-Version: 3.2.1
Mungo-Version: 4.10.3
NodeJS-Version: v6.7.0

Alle 3 Kommentare

Ich hatte auch ein Problem mit der Indexerstellung. In meinem Fall bezog es sich auf schema.options.autoIndex.
schema.options.autoIndex = false für meine Tabellen und ich rufe secureIndexes in einem separaten Prozess auf.
Es wurden jedoch keine Indizes erstellt, da immer noch nach dem autoIndex-Flag gesucht wurde.

Sie können überprüfen, ob #5324 Ihr Problem behebt.

Ich setze autoIndex:false und rufe Model.ensureIndexes() auf, um den Index zu erstellen, aber den Index nicht zu erstellen. Hängt das mit dem Problem zusammen?

'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var uniqueValidator = require('mongoose-unique-validator');

mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017/gh3396',{ config: {autoIndex: false} });

// Variables
var ClientSchema = new Schema(
    {
      name : {
        type     : String,
        required : true,
        trim     : true
      },
      organization : {
        type     : String,
        required : true
      }
    }
  )

// Indexes
ClientSchema.index({ "name" : 1, "organization" : 1 }, { unique : true })

// Plugins
ClientSchema.plugin(uniqueValidator, {
  message : 'Name must be unique.'
})

const Client = mongoose.model('Client', ClientSchema);

Client.ensureIndexes()
Client.on('index', function(err){
console.log(err)
})

new Client({
  name: 'a',
  organization:'a'
}).save().then((result) => {
  console.log(result)
}, (err) => {
  console.log(err)
})

und es erstellt einfach den _id-Index als Standard:

> use gh3396
switched to db gh3396
> show collections
clients
> db.clients.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "gh3396.clients"
    }
]

Irgendwelche Ideen?
MongoDB-Version: 3.2.1
Mungo-Version: 4.10.3
NodeJS-Version: v6.7.0

Ja, das ist ein Duplikat von #5324 und #5328. Wird mit 4.10.6 behoben.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen