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 等级

相关问题

weisjohn picture weisjohn  ·  3评论

simonxca picture simonxca  ·  3评论

varunjayaraman picture varunjayaraman  ·  3评论

CodeurSauvage picture CodeurSauvage  ·  3评论

ghost picture ghost  ·  3评论