Mongoose: インデックスのバグ(インデックスは作成されません)

作成日 2017年06月01日  ·  3コメント  ·  ソース: Automattic/mongoose

こんにちは私はこのような「ニュース」モデルのいいねを保存するために使用されるモデルを持っています:

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;

問題は、インデックスが作成されておらず、 「。on( 'error')」および「.on( 'index')」コールバックからのフィードバックがないことです。これらは実行されていません。

MongoDBバージョン:3.4.2
マングースバージョン:4.10.4
NodeJSバージョン:v6.7.0

最も参考になるコメント

autoIndex:falseを設定し、Model.ensureIndexes()を呼び出してインデックスを作成しましたが、インデックスは作成されません。 それは問題に関連していますか?

'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)
})

デフォルトとして_idインデックスを作成するだけです。

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

何か案は?
MongoDBバージョン:3.2.1
マングースバージョン:4.10.3
NodeJSバージョン:v6.7.0

全てのコメント3件

インデックスの作成にも問題がありました。 私の場合、それはschema.options.autoIndexに関連していました。
私のテーブルではschema.options.autoIndex = falseであり、別のプロセスでensureIndexesを呼び出します。
ただし、autoIndexフラグがチェックされているため、インデックスは作成されませんでした。

#5324で問題が解決するかどうかを確認できます。

autoIndex:falseを設定し、Model.ensureIndexes()を呼び出してインデックスを作成しましたが、インデックスは作成されません。 それは問題に関連していますか?

'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)
})

デフォルトとして_idインデックスを作成するだけです。

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

何か案は?
MongoDBバージョン:3.2.1
マングースバージョン:4.10.3
NodeJSバージョン:v6.7.0

ええ、これは#5324と#5328の複製です。 4.10.6で修正されます。

このページは役に立ちましたか?
0 / 5 - 0 評価