مرحبًا ، لدي نموذج يُستخدم لتخزين الإعجابات لنموذج "الأخبار" مثل هذا:
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
أواجه مشكلة في إنشاء الفهرس أيضًا. في حالتي كان مرتبطًا بـ schema.options.autoIndex.
schema.options.autoIndex = false للجداول الخاصة بي وأنا أطلق عليها ضمان الفهرس في عملية منفصلة.
ومع ذلك ، لم يتم إنشاء الفهارس لأنها لا تزال تتحقق من علامة الفهرس التلقائي.
يمكنك التحقق مما إذا كان # 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.
التعليق الأكثر فائدة
قمت بتعيين autoIndex: false ، واستدعيت Model.ensureIndexes () لإنشاء الفهرس ، لكن الفهرس لا يُنشئ. هل هذا متعلق بالقضية؟
ويقوم فقط بإنشاء فهرس _id كإعداد افتراضي:
أيه أفكار؟
إصدار MongoDB: 3.2.1
إصدار النمس: 4.10.3
إصدار NodeJS: v6.7.0