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')" ์ฝœ๋ฐฑ์—์„œ ํ”ผ๋“œ๋ฐฑ์ด ์—†์–ด์„œ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋ชฝ๊ณ DB ๋ฒ„์ „: 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"
    }
]

์–ด๋–ค ์•„์ด๋””์–ด?
๋ชฝ๊ณ DB ๋ฒ„์ „: 3.2.1
๋ชฝ๊ตฌ์Šค ๋ฒ„์ „: 4.10.3
NodeJS ๋ฒ„์ „: v6.7.0

๋ชจ๋“  3 ๋Œ“๊ธ€

์ธ๋ฑ์Šค ์ƒ์„ฑ์—๋„ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ œ ๊ฒฝ์šฐ์—๋Š” schema.options.autoIndex์™€ ๊ด€๋ จ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚ด ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด schema.options.autoIndex = false์ด๊ณ  ๋ณ„๋„์˜ ํ”„๋กœ์„ธ์Šค์—์„œsureIndexes๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ์—ฌ์ „ํžˆ 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"
    }
]

์–ด๋–ค ์•„์ด๋””์–ด?
๋ชฝ๊ณ DB ๋ฒ„์ „: 3.2.1
๋ชฝ๊ตฌ์Šค ๋ฒ„์ „: 4.10.3
NodeJS ๋ฒ„์ „: v6.7.0

์˜ˆ, ์ด๊ฒƒ์€ #5324 ๋ฐ #5328์˜ ๋ณต์ œ๋ณธ์ž…๋‹ˆ๋‹ค. 4.10.6์œผ๋กœ ์ˆ˜์ •๋ฉ๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰