Mongoose: ์ €์žฅ๋œ ๋ฌธ์„œ๊ฐ€ ์—†์ง€๋งŒ Model.create ์•ฝ์†์ด ํ•ด๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2018๋…„ 02์›” 01์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: Automattic/mongoose

์—ฌ๋Ÿฌ๋ถ„, ์•ˆ๋…•ํ•˜์„ธ์š”,

๋ชฝ๊ตฌ์Šค ๋ฒ„์ „ 5.0.3๊ณผ ๋…ธ๋“œ ๋ฒ„์ „ 9.4.0์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชฝ๊ตฌ์Šค ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด model.create๋Š” ์ƒ์„ฑ๋œ ๋ฌธ์„œ๊ฐ€ DB์— ์ €์žฅ๋  ๋•Œ ํ•ด๊ฒฐ๋˜๋Š” ์•ฝ์†์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์•„๋ž˜ ์ฝ”๋“œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ๋ฌธ์„œ๊ฐ€ DB์— ์ €์žฅ๋˜๊ธฐ ์ „์— ์•ฝ์†์ด ํ•ด๊ฒฐ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค.

async function test(){

    let schema = new mongoose.Schema({a: String});

    let model = mongoose.model('test', schema);

    await model.remove({}).exec();

    await model.create({ a: 'test'}, 
       (err,result)=> {console.log('created');});

    await model.findOneAndUpdate(
        { a : 'test' } , 
        { a: 'newValue'}
    )
    .exec((err, result) => {
        console.log('update : '+result);
    });

    await model.find({a: 'test'},(err,result) => {
        console.log(result); 
    });

}

output in terminal : 
    update : null
    found :
    created

findOneAndUpdate๋Š” ๋ฌธ์„œ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ "created"๊ฐ€ ํ„ฐ๋ฏธ๋„ ๋์— ๋‚˜ํƒ€๋‚˜๋ฏ€๋กœ await๊ฐ€ ๋น„๋™๊ธฐ ์ž‘์—… 'create'๊ฐ€ ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š์€ ๊ฒƒ์ฒ˜๋Ÿผ create ๋ฉ”์„œ๋“œ์˜ ์ฝœ๋ฐฑ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์•„๋ž˜์™€ ๊ฐ™์ด create ์ฝœ๋ฐฑ์ด ํŠธ๋ฆฌ๊ฑฐ๋˜๋ฉด ํ•ด๊ฒฐ๋˜๋Š” promise๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์˜ˆ์ƒํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

async function test(){

    let schema = new mongoose.Schema({a: String});

    let model = mongoose.model('test', schema);

    await model.remove({}).exec();

    await new Promise((resolve,reject) => {
        model.create({ a: 'test'}, 
       (err,result)=> {
            console.log('created');
            resolve();
        });
    });

    await model.findOneAndUpdate(
        { a : 'test' } , 
        { a: 'newValue'}
    )
    .exec((err, result) => {
        console.log('update : '+result);
    });

    await model.find({a: 'newValue'},(err,result) => {
        console.log('found : ' +result); 
    });

}

output in terminal: 
     created
     update : { _id: 5a735fbc1fe826233014d62d, a: 'test', __v: 0 }
     found : { _id: 5a735fbc1fe826233014d62d, a: 'newValue', __v: 0 }

์ด์ œ ์šฐ๋ฆฌ๋Š” ์˜ˆ์ƒํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค.

DB์—์„œ ์ž‘๋™ํ•˜๋Š” ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ํ•ด๊ฒฐ๋œ ๊ฒฝ์šฐ ์ž‘์—…, ํŠนํžˆ ์ฟผ๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์•ฝ์†์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ์ฟผ๋ฆฌ ๊ฐœ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ model.create์˜ ๊ฒฝ์šฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ DB์— ๋ฌธ์„œ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋‹ค๋Š” ํ‘œ์‹œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ดํ–‰๋œ ๋ฐ˜ํ™˜๋œ ์•ฝ์†์ด ๋ฌด์—‡์„ ์˜๋ฏธํ•˜๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค. ์•„๋งˆ๋„ ๋‚˜๋Š” ์ „์ฒด ์š”์ ์„ ๋†“์ณค์ง€๋งŒ ์•ฝ๊ฐ„ ๋ชจํ˜ธํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

4.x ๋ฌธ์„œ์—์„œ model.create์— ์ฝœ๋ฐฑ์„ ์ „๋‹ฌํ•˜๋ฉด ์•ฝ์†์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์ฝ์€ ๊ฒƒ์„ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค.

let x = model.create({ a: 'test' }, () => {})
  x.then(console.log(x)) //TypeError: Cannot read property 'then' of undefined

์ฝœ๋ฐฑ์„ ๊บผ๋‚ด๋ฉด:

let x = model.create({ a: 'test' })
  x.then(console.log(x)) // Promise { <pending> }

model.js์˜ 5.x ์†Œ์Šค๋Š” utils.promiseOrCallback์„ ํ˜ธ์ถœํ•˜์—ฌ ์ด๋ฅผ ๋’ท๋ฐ›์นจํ•ฉ๋‹ˆ๋‹ค.

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

4.x ๋ฌธ์„œ์—์„œ model.create์— ์ฝœ๋ฐฑ์„ ์ „๋‹ฌํ•˜๋ฉด ์•ฝ์†์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์ฝ์€ ๊ฒƒ์„ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค.

let x = model.create({ a: 'test' }, () => {})
  x.then(console.log(x)) //TypeError: Cannot read property 'then' of undefined

์ฝœ๋ฐฑ์„ ๊บผ๋‚ด๋ฉด:

let x = model.create({ a: 'test' })
  x.then(console.log(x)) // Promise { <pending> }

model.js์˜ 5.x ์†Œ์Šค๋Š” utils.promiseOrCallback์„ ํ˜ธ์ถœํ•˜์—ฌ ์ด๋ฅผ ๋’ท๋ฐ›์นจํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ, @lineus ๊ฐ€ ๋งž์Šต๋‹ˆ๋‹ค. @CodeurSauvage ์ด ๋ฌธ์ œ๋ฅผ

@CodeurSauvage @lineus ๊ฐ€ ๋งž์Šต๋‹ˆ๋‹ค. mongoose 5๋Š” ์ฝœ๋ฐฑ์ด ์ง€์ •๋œ ๊ฒฝ์šฐ ์•ฝ์†์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค . ๋‹ค์Œ์„ ๋Œ€์ฒดํ•˜๋ฉด ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

    await model.create({ a: 'test'}, 
       (err,result)=> {console.log('created');});

์™€ ํ•จ๊ป˜:

    await model.create({ a: 'test'}).then(result => console.log('created'));
์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰