๋ค์ ์ฝ๋๋ฅผ ์ค์ด๊ธฐ ์ํด findAndUpdate ๋ฉ์๋๊ฐ ์ ๊ณต๋๊ธฐ ๋๋ฌธ์ ๋๋ค.
Model.findById(_id, function (err, doc) {
if (doc) {
doc.field = 'value';
doc.save(function (err) {
// do something;
});
}
});
์ด์:
.findByIdAndUpdate(_id, {$set: {field: 'value'}}, function (err, doc) {
// do something
});
์ฐ๋ฆฌ๋ pre, post ๋ฏธ๋ค์จ์ด๋ฅผ ์ ํํ ๋์ผํ๊ฒ ์ฌ์ฉํด์ผ ํฉ๋๋ค. ํ์ฌ pre, post ๋ฏธ๋ค์จ์ด๋ findByIdAndUpdate๋ฅผ ๋ง๋ค ๋ ์คํ๋์ง ์์ต๋๋ค.
๋์์ธ์ ์ํด. ํํฌ๋ฅผ ํธ์ถํ๋ ๋ฐ ๊ด๋ จ๋ ๋ฌธ์๊ฐ ์์ต๋๋ค.
์์ , ํํฌ๋ฅผ ํธ์ถํ ๋ฌธ์๊ฐ ์์ต๋๋ค.
๊ทธ๋์? ์ฌ์ , ์ฌํ ๋ฏธ๋ค์จ์ด๋ฅผ ํธ์ถํ๋ ค๋ฉด ์ฒซ ๋ฒ์งธ ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํด์ผ ํฉ๋๊น?
๋ค ๋ง์ต๋๋ค. Model.update,findByIdAndUpdate,findOneAndUpdate,findOneAndRemove,findByIdAndRemove๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ง์ ์คํ๋๋ ๋ช ๋ น์ ๋๋ค.
์ด๊ฒ์ ๊ฐ์ด๋ ์ ๋ช
ํํ ๋ช
์๋์ด ์์ด์ผ ํฉ๋๋ค. ํนํ ๋์ผํ ํ์ด์ง์์ ์ ํจ์ฑ ๊ฒ์ฌ์ ๋ํด ๋งํ๊ณ findByIdAndUpdate
๋ฅผ "๋ ๋์" ๊ฒ์ผ๋ก ์ค๋ช
ํ๋ ๊ฒฝ์ฐ ๋์ฑ ๊ทธ๋ ์ต๋๋ค.
"๋ ๋์" ๋งํฌ๋ฅผ ํด๋ฆญํ๋ฉด ์ ์ฒด ๋ฌธ์๋ก ์ด๋ํฉ๋๋ค.
์ ํจ์ฑ ๊ฒ์ฌ ๋ฑ์ ์ค๋ช
ํ๋ ๋ฐฉ๋ฒ
๋ ๋ซ๋ค๊ณ ์๊ฐ๋๋ ๊ฒ์ ์ถ๊ฐํ๊ธฐ ์ํด ํ ๋ฆฌํ์คํธ๋ฅผ ๋ณด๋ด์ฃผ์ญ์์ค. ๊ทธ๊ฒ์
๋งค์ฐ ์ฝ์ต๋๋ค.
https://github.com/LearnBoost/mongoose/blob/master/CONTRIBUTING.md
2012๋ 10์ 31์ผ ์์์ผ ์คํ 6์ 3๋ถ, Jesse Fulton [email protected] ์์ฑ:
์ด๊ฒ์ ๊ฐ์ด๋http://mongoosejs.com/docs/documents.html ์์ ํ์คํ ๋ช ์๋์ด์ผ ํฉ๋๋ค.
ํนํ ๋์ผํ ํ์ด์ง์์ ์ ํจ์ฑ ๊ฒ์ฌ์ ๋ํด ์ด์ผ๊ธฐํ๋ ๊ฒฝ์ฐ
findByIdAndUpdate๋ฅผ "๋ ๋์" ๊ฒ์ผ๋ก ์ค๋ชโ
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ Gi tHubhttps://github.com/LearnBoost/mongoose/issues/964#issuecomment -9967865์์ ํ์ธํ์ธ์.
์๋ก
@aaronheckmann https://twitter.com/#!/aaronheckmann
๋ฏธ๋ค์จ์ด ๋ฌธ์์ ๋ฉ๋ชจ ์ถ๊ฐ
ํ ๋ฆฌํ์คํธ: https://github.com/LearnBoost/mongoose/pull/1750
์๋ ํ์ธ์,
๋ฌธ์ ๊ฐ ์ข ๋ฃ๋์๋ค๋ ๊ฒ์ ์๊ณ ์์ง๋ง ๋ต๋ณ์ ์์ ํ ๋ง์กฑํ์ง ์์ต๋๋ค. findOneAndUpdate ๋ฐ ๊ธฐํ ์ ์ฌํ ์์ ์ ์ํ ์ฌํ ์ ์ฅ ๋๋ ์ฌํ ์ ๋ฐ์ดํธ ๋ฏธ๋ค์จ์ด๊ฐ ์์ด์ผ ํ์ง ์์ต๋๊น? ๋ฌธ์๊ฐ ๋ฐํ๋์์ผ๋ฏ๋ก ๊ด์ฐฎ์ ๋ณด์ ๋๋ค. ์ฌ์ ๋ฏธ๋ค์จ์ด ๋๋ Model.update์๋ ์ ํฉํ์ง ์์ต๋๋ค. ๋์ํฉ๋๋ค.
ํ์ฌ ์ง์ํด์ผ ํ๋ ์ผ๋ถ ์์ ์ ๋ํด ๋ธ๋ผ์ธ๋์ธ mongoosastic๊ณผ ๊ฐ์ ํ๋ฌ๊ทธ์ธ์ ๊ธฐ๋ฅ์ ํฌ๊ฒ ํฅ์์ํฌ ๊ฒ์ ๋๋ค.
๋ฏธ๋ค์จ์ด๊ฐ ์๋ค๋ฉด ํ๋ฌ๊ทธ์ธ์์ ์ผ๋ถ ์ฌํ ์ ๋ฐ์ดํธ ์์ ์ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ด๋์ด๊ฐ ์์ต๋๊น?
๊ฐ์ฌ ํด์
@albanm ํน์ ๋ฐฉ๋ฒ์ ๋ชฝ๊ตฌ์ค๋ฅผ ์์ ํ ์ฐํ ํ๋ฏ๋ก ๋ฏธ๋ค์จ์ด ํํฌ๋ฅผ ์ป์ง ๋ชปํฉ๋๋ค. AFAIK, ํํฌ๋ฅผ ์คํํ๋ ์ ์ผํ ๋ฐฉ๋ฒ์ ์์์ ์ธ๊ธํ ๊ฒ์ฒ๋ผ ๋ณ๋์ find()
๋ฐ save()
ํธ์ถ์ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค.
๋๋ ๊ทธ๊ฒ์ ์ดํดํ๊ณ ์๋ฏธ๊ฐ ์์ต๋๋ค. ํน์ ๋ฐฉ๋ฒ ์ด์ ์ ๊ฐ์ ธ์ค๊ธฐ๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ฌ์ ๋ฏธ๋ค์จ์ด๋ ์์ฌ์ ์ฌ์ง๊ฐ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ฌ์ ํ mongoose๋ ์ ๋ฐ์ดํธ๋ ๋ฌธ์๋ฅผ ๋ฐํํ๊ณ ์ผ๋ถ ํฌ์คํธ ํํฌ๋ฅผ ํธ๋ฆฌ๊ฑฐํ๋ ์ ๋ฐ์ดํธ ์์ ์ ๋ํํ ์ ์์ด์ผ ํฉ๋๋ค.
@albanm ์ด ๋ง๋ค๊ณ ์๊ฐํ๋๋ฐ, ์ฌ๋๋ค์ ๊ฐ์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ๋ ๊ฐ์ ๊ธฐ๋ฅ์ ์ํ ์ ์์ต๋๋ค. ํํฌ๊ฐ ์๋์ง ํ์ธํ๋ ์ผ๋ถ ์ฐจ๋จ์ผ๋ก '์ง์ ์ ๋ฐ์ดํธ' ๋ฉ์๋๋ฅผ ๋ํํ๋ ๊ฒ์ ์ด๋ป์ต๋๊น? ํํฌ๊ฐ ์์ผ๋ฉด ์ฌ์ฉํ๊ฑฐ๋ ๊ทธ๋ ์ง ์์ผ๋ฉด ์๋ ์ ๋ฐ์ดํธ ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค.
+1
+1
+1
+1
:+1:
+1
:-1:
๊ธฐ๋ฅ ์์ฒญ์ ๋ฐ์์ง๋ง ๋ฏธ๋ค์จ์ด๋ก ๋ ์ค๋ ์์
ํ ์๋ก db ์
๋ฐ์ดํธ ์คํฌ๋ฆฝํธ ๋ฐ ์ผ์์ ์ธ ์์
์ ์ผ์์ ์ด์ง ์์ ๊ธฐํ ํญ๋ชฉ์ ํธ์ถํ ๋ ์ข
์ข
๋ฏธ๋ค์จ์ด๋ฅผ ์ฐํํด์ผ ํ๋ ์์ ์ ๋ฐ๊ฒฌํฉ๋๋ค. ์ ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์์ ๊ธฐ๋ฅ ์์ฒญ์ ์ด๋ฏธ ๊ตฌํํ๋ ์ฌ์ฉ์ ์ง์ ๋ํผ๋ฅผ ๋ง๋๋ ๊ฒ์ด ๋งค์ฐ ์ฌ์์ง๋๋ค. mongoose๋ ์ด์ ๋ฒ์ 4์ ๋ํ ์์ด๋์ด๋ฅผ ๊ณต๊ฐํ๊ณ ์์ผ๋ฏ๋ก v3์์ ์ด ์ ๋์ API ๋ณ๊ฒฝ์ด ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ ๊ฑฐ์ ์์ต๋๋ค. ์ด๊ฒ์ v4 ํ ๋ก ์ผ๋ก ์ฎ๊ฒจ์ง๊ธฐ๋ฅผ ์์ฒญํฉ๋๋ค.
๊ทธ๋ฌ๋ :+1: ์ ์ฅ ํธ์ถ์์ ๋ฏธ๋ค์จ์ด๋ฅผ ๋นํ์ฑํํ ์ ์๋ค๋ฉด. ๋๋ ์ข ์ข ๋ค๋ฅธ ํจ์์์ ์ ๋ฌ๋ ๋ชฝ๊ตฌ์ค ๊ฐ์ฒด๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋จ์ํ .save๋ฅผ ์ํํ๊ณ ์ถ์ดํฉ๋๋ค. ์ด ์ํฉ์์๋ ์ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ๋ ๊ฒ๋ณด๋ค .save๋ฅผ ์ํํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ์ด๊ฒ์ด ๊ฐ๋ฅํ๋ค๋ฉด ์ง์ ํด์ฃผ์ธ์.
์ด๋ ์ชฝ์ด๋ , ๋๋ผ์ด ๋์๊ด. ๋ฉ์ง ๊ด๋ฆฌ์์๊ฒ ๊ฒฝ์๋ฅผ ํํฉ๋๋ค. ๊ทธ๊ฒ ์์ด๋ ์ด๋ป๊ฒ ์๋ํ ์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
+1
์ด๋ฌํ ๋ฉ์๋์ ๋ํ ํํฌ๋ฅผ ์ถ๊ฐํ๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ ๋ด์ฅ ํจ์๋ฅผ ๋ฎ์ด์ฐ๋ ๊ฒ์ ๋๋ค.
_update = UserModel.update
UserModel.update = (conditions, update) ->
update.updatedAt or= new Date
_update.apply this, arguments
๊ทธ๋ฐ ๋ค์ ๋ชฝ๊ตฌ์ค์ ๋ชจ๋ ์ ๋ฐ์ดํธ ํธ์ถ์ ๋ฐ์ดํฐ์ updatedAt ํค๋ฅผ ์์ ํฉ๋๋ค.
์ด ๋ชจ๋ธ ๋ฆผ๋ณด ๋ฅผ ์๋ํด ๋ณผ ์ ์์ต๋๋ค. ๋ชจ๋ ์คํค๋ง์ ์ ์ /๋ฉ์๋/๋ฎ์ด์ฐ๊ธฐ ๋ฐ์ธ๋ฉ์ ์ง์ํ๊ณ mongodb๋ฅผ ์ฟผ๋ฆฌํ๊ธฐ ์ํด rpc ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๋ชฝ๊ตฌ์ค ๋ชจ๋ธ์ ๊ฐ๋จํ ๋ํผ์ ๋๋ค.
+1, ์ด ๊ธฐ๋ฅ์ด ํ์ํฉ๋๋ค...
์ถ๋ก ์ ์ด๋ ์ ๋ ์ดํดํ์ง๋ง ์์ ์ ๋ฐ์ดํธ IMHO์ ๋ํ ํํฌ๊ฐ ์๊ธฐ ๋๋ฌธ์ Mongoose๋ ์ค์ ๋ก ๋ค์ ๋ฌด์๋ฏธํฉ๋๋ค. ์์ ์ ๋ฐ์ดํธ๋ฅผ ์ฌ์ฉํ ๋ ์ ํจ์ฑ ๊ฒ์ฌ, ๊ธฐ๋ณธ๊ฐ ๋ฑ์ด ์คํ๋์ง ์์ผ๋ฏ๋ก ODM์ ์ฌ์ฉํ๋ ค๋ ์ ์ฒด ๋ชฉ์ ์ด ๋ฌดํจํ๋ฉ๋๋ค. ์ฐพ๊ธฐ/์ ์ฅ์ ์ฌ์ฉํ๋ฉด ์์ ์ด ์ํ๋์ง๋ง ์ด๊ฒ์ด ํญ์ ์ฌ์ฉ๋๋ค๋ ๋ณด์ฅ์ด ์์ต๋๊น?
๋ํ, ์ผ๋ฐ์ ์ผ๋ก ์์์ ์ฐ์ฐ์ด ์๋๊ธฐ ๋๋ฌธ์ ์ฐพ๊ธฐ/์ ์ฅ์ ํผํ๋ ค๊ณ ํฉ๋๋ค. MongoDB๋ ๊ฐ๋ ฅํ ์์ ์ฟผ๋ฆฌ ๋ฐ ์
๋ฐ์ดํธ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ฌ ํธ๋์ญ์
์ง์ ๋ถ์กฑ์ ๋ณด์ํฉ๋๋ค. ๋ฐ๋ผ์ ์ด๋ฌํ ์์์ ์์
์ ์ฌ์ฉํ์ง๋ง ๋ฏธ๋ค์จ์ด ์ง์ ์์ด Mongoose๋ ๊ธฐ๋ณธ MongoClient
๋ณด๋ค ๋ง์ ๊ฐ์น๋ฅผ ์ ๊ณตํ์ง ์์ต๋๋ค.
http://aaronheckmann.tumblr.com/post/48943525537/mongoose-v3-part-1-versioning ์ ์์ ๋ update
๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ๋ฏธ๋ค์จ์ด๋ฅผ ์ฐํํฉ๋๋ค. ๋ฒ์ ๊ด๋ฆฌ ๋๋ ๋ฏธ๋ค์จ์ด๋ฅผ ์ ์ ํ๊ฒ ์ฌ์ฉํ ์ ์์ง๋ง ๋ ๋ค ๊ฒฐํฉํ ์๋ ์์ต๋๊น? ์ ๋ง, ๊ทธ๊ฒ์ ๊ฐ์ง๊ณ ์์ ์ด ์ด๋ ์์ต๋๊น?
๋๋ ๊ธฐ์ ์ ์ธ ์ด์ ์กฐ์ฐจ ์์ ํ ์ดํดํ์ง ๋ชปํฉ๋๋ค: If update
& co. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์
์ ๋๋ฌ์ธ๊ณ ์ค์ ๋ก ์
๋ฐ์ดํธ๋ฅผ ์ํํ๊ธฐ ์ ์ ์ผ๋ถ ์ ํจ์ฑ ๊ฒ์ฌ/์ฌ์ฉ์ ์ง์ ์ ์ํํ ์ ์๋๋ก ํธ์ถ์ ๊ฐ๋ก์ฑ๊ณ ์ฟผ๋ฆฌ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ ์ ์๋ ์ด์ ๋ ๋ฌด์์
๋๊น?
@joerx +1์ด๋ฉด ์ถฉ๋ถํฉ๋๋ค. :) ํ์ง๋ง ๋น์ ์ ์ถ๋ก ์ ์๋ฒฝํฉ๋๋ค.
3.9.x ๋ธ๋์น๋ find
๋ฐ findOne
์ ๋ํ ์ฌ์ ๋ฐ ์ฌํ ํํฌ๋ฅผ ์ง์ํฉ๋๋ค findOneAndUpdate
๋ฐ update
์ ๋ํ ์ง์์ ์ฝ๊ฒ ์ถ๊ฐํ ์ ์์ด์ผ ํฉ๋๋ค.
์ด ๊ธฐ๋ฅ์ด ๋ณํฉ๋์์ต๋๊น?
๋ฐ๋ผ์ pre('findOneAndUpdate')
๋ฐ post('findOneAndUpdate')
ํํฌ๋ ๋ง์คํฐ์ ์์ผ๋ฉฐ ์์ง ์
๋ฐ์ดํธ ํํฌ๊ฐ ์์ต๋๋ค. ์์ง ๋ ์ค ํ๋๋ฅผ ํฌํจํ๋ ๋ฆด๋ฆฌ์ค๊ฐ ์์ต๋๋ค.
์ด์ .update() ์ดํ์ ์ฌ์ ์ ์ฅ์ ํธ๋ฆฌ๊ฑฐํฉ๋๊น?
์๋์. Query.update()
์ ๋ํ ๋ณ๋์ update()
ํํฌ๊ฐ ์์ต๋๋ค. ์ ์ฅ ํํฌ๋ ์ฟผ๋ฆฌ ํํฌ์ ๋ค๋ฆ
๋๋ค.
@vkarpov15 ์
๋ฐ์ดํธ ํํฌ์ ๋ํ ์ง์ ๋ฌธ์๋ฅผ ๋งํฌํ ์ ์์ต๋๊น? pre('findOneAndUpdate')
๋ฐ post('findOneAndUpdate')
๊ฐ ์ธ์ ์ถ์๋ ์ง์ ๋ํ ์์์ด ์์ต๋๊น?
@karlstanton ์ฌ์ฉ 4.0.0-rc2, npm install mongoose@unstable
:)
'use strict';
var Promise = require('bluebird');
var mongoose = Promise.promisifyAll(require('mongoose'));
var counterSchema = new mongoose.Schema({
total: {
type: Number,
default: 0
}
});
counterSchema.post('findOneAndUpdate', function (doc) {
console.log(doc.total);
});
var Counter = mongoose.model('Counter', counterSchema);
Promise.coroutine(function *() {
yield mongoose.connectAsync(process.env.MONGODB_URI);
console.log('Connected');
let counter = yield Counter.createAsync({});
console.log(`${counter.total}`);
for (let i = 0; i < 10; i++) {
yield Counter.findOneAndUpdateAsync({ _id: counter.id }, { $inc: { total: 1} });
}
})();
0
0
1
2
3
4
5
6
7
8
9
ํ๋ฐ์ง ๋ฌผ๋ฌ๋๋ฏ ํฉ๋๋ค. ๋ด๊ฐ ๋ฌด์์ ๋์น๊ณ ์์ต๋๊น?
์, new
ํ๋๊ทธ๋ ๊ธฐ๋ณธ ๋ธ๋ ์ดํน ์ฒด์ธ์ง์
๋๋ค. ๊ธฐ๋ณธ ๋๋ผ์ด๋ฒ์์ ์ผ๊ด์ฑ์ ๋ํด ํ ๋ง์ด ์์ง๋ง ํนํ ์ด ์๋ก์ด ํํฌ๋ฅผ ๊ณ ๋ คํ ๋ ์ง๊ด์ ์ด์ง ์์ต๋๋ค.
@๋ค๋ฒํญ์ค :)
๋ค @neverfox ์ ์ก์์ต๋๋ค. ์ด ๋ณ๊ฒฝ ์ฌํญ์ ๋ฆด๋ฆฌ์ค ์ ๋ณด์ ๋ฌธ์ํ๋์ด ์์ผ๋ฉฐ #2262์์ ๋ณ๊ฒฝ๋ ์ด์ ์ ๋ํ ์์ธํ ์ค๋ช ์ ๋ณผ ์ ์์ต๋๋ค.
๊ทธ ์ด์ ๋ mongodb ๋
ธ๋ ๋๋ผ์ด๋ฒ, mongodb ์
ธ, ์ค์ mongodb ์๋ฒ ๋ฑ์์ new
๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก false์ด๊ณ ๋นํ์ค ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ๋ ๋ํผ ๊ณ์ธต์ด ๊ฐ๋ฐ์์ ์ถ์ ์ด๋ ต๊ฒ ๋ง๋ค๊ธฐ ๋๋ฌธ์
๋๋ค. ์ด ๋์ฐํ๊ฒ ์๋ชป๋ ๋ชจ๋์ ์ ์ ์๋ gulp-uglify ์
๋๋ค. ์ด๋ uglify-js์ ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ ์ํฉ๋๋ค.
๋ฌธ์ ๊ฐ ์ข ๋ฃ๋์์ง๋ง ๋ฒ์ 4.0.2์ ์๋ ๊ธฐ๋ฅ์ด ์์ง ๋ถ์์ ํ ๋ฒ์ ์ ์๋ค๋ ๊ฒ์ ์ ์ ์์ต๋๊น? Scema.pre('update') ๋๋ Schema.pre('findOneAndUpdate') 4.0.2 ๋ฒ์ ์ findOneAndUpdate์์ ์คํ๋์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ํจ์์ ์ ๋ฌํด์ผ ํ๋ ํญ๋ชฉ์ด ๋๋ฝ๋์์ต๋๊น?
@CaptainStaplerz ํ๋ฆฌํ ์ ์ด๋ป๊ฒ ์ ์ธํ๊ณ ์์ต๋๊น?
findOneAndUpdate ๋ฏธ๋ค์จ์ด ํํฌ๋ฅผ 4.0.2์์ ์ฌ์ฉํ ์ ์์ต๋๊น? ์ด๊ฒ์ ์ฌ์ฉํ๊ธฐ ์ํด 3.8์์ ์ต์ mongoose 4.0.2๋ก ์ ๊ทธ๋ ์ด๋ํ์ผ๋ฉฐ ๋ฏธ๋ค์จ์ด Document.schema.post('findOneAndUpdate', function (doc)๊ฐ save() ๋๋ remove()์ฒ๋ผ ํธ๋ฆฌ๊ฑฐ๋์ง ์์ต๋๋ค.
@honitus ์ฝ๋๋ฅผ ๋ณด์ฌ์ฃผ์ธ์
@vkarpov15 - ๋น ๋ฅธ ์๋ต์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
๋ธ๋ก๊ทธ.์ปจํธ๋กค๋ฌ,js
// Updates an existing Blog in the DB, adds comment
exports.update = function(req, res) {
Blog.findOneAndUpdate(
{"_id": req.body._id,},
{"$push": {"comments": buildComment}},
{safe: true, upsert: true}, function (err, workspace) {
if (err) {
return handleError(res, err);
}
return res.send(200);
}
);
}
๋ธ๋ก๊ทธ.์์ผ.js
/**
* Broadcast updates to client when the model changes
*/
'use strict';
var Blog= require('./blog.model');
exports.register = function(socket) {
//SAVE WORKS
Blog.schema.post('save', function (doc) {
onSave(socket, doc);
});
// IS NOT TRIGGERED :(
Blog.schema.post('findOneAndUpdate', function (doc) {
onComment(socket, doc);
});
Blog.schema.post('remove', function (doc) {
onRemove(socket, doc);
});
}
//SAVE WORKS when a new blog is created
function onSave(socket, doc, cb) {
socket.emit('blog:save', doc);
}
// IS NOT TRIGGERED :(
function onComment(socket, doc, cb) {
socket.emit('blog:findOneAndUpdate', doc);
}
function onRemove(socket, doc, cb) {
socket.emit('blog:remove', doc);
}
@vkarpov15 ๋ค์ ์ด์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ์ด๊ฒ์ findOneAndUpdate์ ๋ํ ํํฌ๊ฐ 4.0.2์ ์๊ณ 4.0.3์ ํฌํจํ ๊ณํ์์ ์๋ฏธํฉ๋๊น?
@vkarpov15 ๋ค์์ ํํฌ๋ฅผ ์ ์ธํ๋ ์ฝ๋์ ๋๋ค.
...
var TodoSchema = new mongoose.Schema({
name: {type: String, required: true},
note: String,
completed: {type: Boolean, default: false},
updatedAt: {type: Date, default: Date.now},
user: {
type: mongoose.Schema.ObjectId,
ref: 'Users'
}
});
...
// Not executed
TodoSchema.pre('update', function() {
console.log('------------->>>>>> update updatedAt')
this.updatedAt = Date.now();
});
// Not executed
TodoSchema.pre('findOneAndUpdate', function() {
console.log('------------->>>>>> update updatedAt')
this.updatedAt = Date.now();
});
๊ทธ๋ฆฌ๊ณ ์ฌ๊ธฐ์์ ์ ๋ฐ์ดํธ๋ฅผ ํธ์ถํฉ๋๋ค.
...
router.route('/:id')
.put(function(req, res, next) {
TodoModel.findOneAndUpdate({_id: req.params.id, user: req.user.id}, req.body, {new: true}, function(err, post) {
if(err) return next(err);
if(post) {
res.status(200).json(post);
}
else {
next(newSystemError(errorCodes.TODO_NOT_FOUND, undefined, req.params.id));
}
});
});
๋ด๊ฐ ์๋ ํ findOneAndUpdate()
ํํฌ๊ฐ ์์ด์ผ ํฉ๋๋ค. ์๋ํ์ง ์์ผ๋ฉด ๋ฒ๊ทธ์
๋๋ค.
@CaptainStaplerz ์๋:
TodoSchema.pre('update', function(next) {
console.log('------------->>>>>> update updatedAt')
this.updatedAt = Date.now();
next();
});
๋ํ console.log
๋ฌธ์ด ์คํ ์ค์
๋๊น, ์๋๋ฉด ์์์น ๋ชปํ ๊ฒฐ๊ณผ๋ฅผ ์ฃผ๋ Date.now() ๋ถ๋ถ์
๋๊น?
@vkarpov15
Add implementation #964 https://github.com/Automattic/mongoose/commit/e98ef98e857965c4b2ae3339fdd7eefd2a5a9913 ์์ ๋ณ๊ฒฝํ ์ฌํญ์ ์ถ๊ฐํ์ฌ ์์ค ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ์ต๋๋ค.
๊ทธ๊ฒ์ ์ง๊ธ ๋งค๋ ฅ์ฒ๋ผ ์๋ํฉ๋๋ค. ๊ทธ๋์ ์์ ์ฌํญ์ด ๋ฉ์ธ์ ์ฒดํฌ์ธ๋์ง ์์๋ค๊ณ ์๊ฐํฉ๋๋ค.
@honitus mongoose 4.0.2๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๊น? ํด๋น ๋ณ๊ฒฝ ์ฌํญ์ ์ค์ ๋ก 4.0.0 ์ด์์ ์ ์ฉ๋ฉ๋๋ค.
honitus$ npm ๋ณด๊ธฐ ๋ชฝ๊ตฌ์ค ๋ฒ์
4.0.2
@honitus ๋น์ ์ด ์๋ชปํ๊ณ ์๋ ๊ฒ์ ๋ชจ๋ธ์ ์ปดํ์ผ ํ ํ ์คํค๋ง์ ํํฌ๋ฅผ ์ถ๊ฐํ๊ณ ์๋ค๋ ๊ฒ์
๋๋ค. Model.schema.pre('remove');
๋ ์๋ํ์ง ์์ ๊ฒ์ผ๋ก ์์๋ฉ๋๋ค. ๋ชจ๋ธ ๋ฌธ์์์ "์ฒซ ๋ฒ์งธ ๋ชจ๋ธ ์ปดํ์ผ"์ ์ฐธ์กฐํ์ธ์ . ๋จผ์ ํํฌ๋ฅผ ์คํค๋ง์ ์ฐ๊ฒฐํ๋ฉด ์๋ํด์ผ ํฉ๋๋ค. ์ด๊ฒ์ด ์ฝ๋์ ํ
์คํธ ์ฌ์ด์ ์ ์ผํ ์ฐจ์ด์ ์
๋๋ค.
@CaptainStaplerz ๊ทํ์ ์ฝ๋๋ฅผ ์ฌํํ ์ ์๋ ์ ์ผํ ๋ฐฉ๋ฒ์ ๋น ์ ๋ฐ์ดํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค. ์๋ ์ฝ๋๊ฐ ์๋ํฉ๋๋ค
var mongoose = require('mongoose');
mongoose.set('debug', true);
var util = require('util');
mongoose.connect('mongodb://localhost:27017/gh964');
var TodoSchema = new mongoose.Schema({
name: {type: String, required: true},
note: String,
completed: {type: Boolean, default: false},
updatedAt: {type: Date, default: Date.now},
user: {
type: mongoose.Schema.ObjectId,
ref: 'Users'
}
});
TodoSchema.pre('update', function() {
console.log('------------->>>>>> update updatedAt')
this.updatedAt = Date.now();
});
TodoSchema.pre('findOneAndUpdate', function() {
console.log('------------->>>>>> update updatedAt')
this.updatedAt = Date.now();
});
var Todo = mongoose.model('Todo', TodoSchema);
Todo.update({}, { note: "1" }, function(err) {
if (err) {
console.log(err);
}
console.log('Done');
process.exit(0);
});
๊ทธ๋ฌ๋ ์ ๋ฐ์ดํธ๋ฅผ ๋น์ฐ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ฉ๋๋ค.
var mongoose = require('mongoose');
mongoose.set('debug', true);
var util = require('util');
mongoose.connect('mongodb://localhost:27017/gh964');
var TodoSchema = new mongoose.Schema({
name: {type: String, required: true},
note: String,
completed: {type: Boolean, default: false},
updatedAt: {type: Date, default: Date.now},
user: {
type: mongoose.Schema.ObjectId,
ref: 'Users'
}
});
TodoSchema.pre('update', function() {
console.log('------------->>>>>> update updatedAt')
this.updatedAt = Date.now();
});
TodoSchema.pre('findOneAndUpdate', function() {
console.log('------------->>>>>> update updatedAt')
this.updatedAt = Date.now();
});
var Todo = mongoose.model('Todo', TodoSchema);
Todo.update({}, { }, function(err) {
if (err) {
console.log(err);
}
console.log('Done');
process.exit(0);
});
์ฌ์ ์ ๋ฐ์ดํธ ํํฌ๊ฐ ๋ ์ด์ ์คํ๋์ง ์์ต๋๋ค. ์ด๊ฒ์ด ๋น์ ์ด ๋ณด๊ณ ์๋ ๊ฒ๊ณผ ์ผ์นํฉ๋๊น?
@vkarpov15
Model.schema.pre('์ ๊ฑฐ'); Angular ์ ์ฒด ์คํ์ ์ํด ์๋์ผ๋ก ์์ฑ๋ ์คํ
์ค ํ๋์
๋๋ค.
๊ตฌํ์ ์ถ๊ฐํ์ต๋๋ค. #964 e98ef98
๋ด๊ฐ ํ ๊ฒ์ ์๋์ ํ ์ค์ ์ถ๊ฐํ๋ ๊ฒ์
๋๋ค.
this._schema.s_.hooks.wrap('findOneAndUpdate', Query.base.findOneAndUpdate,
4.0.2์ 1526ํ ์ฐธ์กฐ - this.model.hooks.wrap('findOneAndUpdate', Query.base.findOneAndUpdate,
1526ํ์ this.schema.s.hooks.wrap('findOneAndUpdate', Query.base.findOneAndUpdate,
์๋ํฉ๋๋ค.
๊ทธ๊ฒ์ ์๋์ ์ผ๋ก ์ค๊ณ๋ ๊ฒ์
๋๋ค. Mongoose๋ ๋ชจ๋ธ์ด ์ปดํ์ผ๋ ํ(์ฆ, mongoose.model()
ํธ์ถ) ์คํค๋ง ํํฌ๋ฅผ ์์ ํ๋ ๊ฒ์ ํ์ฉํ์ง ์์ต๋๋ค. ์ด๊ฒ์ด ๋ ์ด์ ์๋ํ์ง ์๋ ์ด์ ์
๋๋ค.
@vkarpov15 ์ด์ ๋ค์ ์์ ์ผ๋ก ํํฌ๋ฅผ ์๋์ํฌ ์ ์์์ต๋๋ค. ์ด๋ฆฌ์์ ์คํ ๋๋ ๋ค๋ฅธ ๊ฒ์ด์์ ๊ฒ์ ๋๋ค.
TodoSchema.pre('findOneAndUpdate', function(next) {
console.log('------------->>>>>> update updatedAt: ', this.updatedAt);
this.updatedAt = Date.now();
next();
});
๊ทธ๋ฌ๋ 'this'๋ ์ ๋ฐ์ดํธ๋ ๋ชจ๋ธ์ ์ฐธ์กฐํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค(๊ทธ๋ฌ๋ '์ ์ฅ' ํํฌ์์๋ ์ฐธ์กฐํฉ๋๊น?). ๋ฐ๋ผ์ this.updatedAt๋ ์ ์๋์ง ์์์ ์ฐธ์กฐํฉ๋๋ค.
'findOneAndUpdate' -hook์์ 'updatedAt' -ํ๋๋ฅผ ์ด๋ป๊ฒ ์ ๋ฐ์ดํธํฉ๋๊น?
์ด๊ฒ์ ๋ช
ํํ ํ๋ ๋ ๋ง์ ๋ฌธ์๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค findOneAndUpdate
ํธ์ถํ ๋ ์
๋ฐ์ดํธ ์ค์ธ ๋ฌธ์๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์์ ์ ์์ผ๋ฏ๋ก this
๊ฐ์ฒด๋ ์ฟผ๋ฆฌ ๋ฏธ๋ค์จ์ด ์ ๋ฌธ์ ๊ฐ ์๋ ์ฟผ๋ฆฌ ๋ฅผ ์ฐธ์กฐํฉ๋๋ค. this.update({ $set: { updatedAt: Date.now() } });
์๋
@vkarpov15 this.update({ $set: { updatedAt: Date.now() } });
๋ฅผ ์๋์ํค์ง ๋ชปํ์ง๋ง findOneAndUpdate
this._update['$setOnInsert'].updatedAt=Date.now();
๋ก ์ฑ๊ณต์ ์ผ๋ก ์
๋ฐ์ดํธํ ์ ์์์ต๋๋ค.
๊ทธ๋ฐ ๋ด๋ถ ์ํ๋ฅผ ์กฐ์ ํ๋ ๊ฒ์ ์์กดํ์ง ์๋ ๊ฒ์ด ์ข์ต๋๋ค. this.update()
์ด ์๋ํ์ง ์๋๋ค๋ ์ฌ์ค์ ์๋นํ ๋๋์ต๋๋ค. ํํฌ๊ฐ ์ด๋ป๊ฒ ์๊ฒผ๋์ง ๋ณด์ฌ์ฃผ์๊ฒ ์ต๋๊น?
ํ์ ํ๋! (์ฐธ๊ณ ๋ก ์ ๋ 4.0.2๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค)
tagSchema.pre('findOneAndUpdate',function(next){
var self = this;
//NOTE THAT 'this' in the findOneAndUpdate hook refers to the query, not the document
//https://github.com/Automattic/mongoose/issues/964
geoData.country.findOne({'_id':self._update['$setOnInsert'].countryCode}).select('_id name cca2 cca3 ccn3').lean().exec(function(err,country){
if (err){throw err;}
if (!country){throw 'no coutnry';}
self._update['$setOnInsert'].country=country;
next();
});
});
๋ด ์ฑ์ ๋ค๋ฅธ ๊ณณ์์ ๋ฌธ์๋ฅผ ์ด๊ธฐํํ ๋ ๋ถ๋ช ํ ์ด๊ฒ์ ์ฒ๋ฆฌํ ์ ์์ง๋ง, ๋ชฝ๊ตฌ์ค์ ๋ชจ๋ ๊ฒ์ด ํฌํจ๋์ด ์๋ค๋ ๊ฒ์ด ์ข์ต๋๋ค. ์ด๋ค ์๊ฐ์ด๋ ํ์ํฉ๋๋ค!
์, ํผ๋์ค๋ฌ์์ update
๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค๊ณ ์๊ฐํ์ต๋๋ค. ์๋ ์คํฌ๋ฆฝํธ
var mongoose = require('mongoose');
mongoose.set('debug', true);
var util = require('util');
mongoose.connect('mongodb://localhost:27017/gh964');
var TodoSchema = new mongoose.Schema({
name: {type: String, required: true},
note: String,
completed: {type: Boolean, default: false},
updatedAt: {type: Date, default: Date.now},
user: {
type: mongoose.Schema.ObjectId,
ref: 'Users'
}
});
TodoSchema.pre('findOneAndUpdate', function() {
this.findOneAndUpdate({}, { updatedAt: Date.now() });
});
var Todo = mongoose.model('Todo', TodoSchema);
Todo.findOneAndUpdate({}, { note: "1" }, function(err) {
if (err) {
console.log(err);
}
console.log('Done');
process.exit(0);
});
์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ๊ณ ์ํ๋ ์ฟผ๋ฆฌ๋ฅผ ์คํํฉ๋๋ค.
Mongoose: todos.findAndModify({}) [] { '$set': { note: '1', updatedAt: new Date("Thu, 07 May 2015 20:36:39 GMT") } } { new: false, upsert: false }
Done
๋ฐ๋ผ์ ์ฌ์ฉํ์ญ์์ค
TodoSchema.pre('findOneAndUpdate', function() {
this.findOneAndUpdate({}, { updatedAt: Date.now() });
});
mquery์ ๋ด๋ถ ์ํ๋ฅผ ์๋์ผ๋ก ์กฐ์ํ๋ ๋์ - ์ค์ ๋ก ์ํ ์ค์ธ ์์ ์ ์ค์ ๋ก ์์ง ๋ชปํ๋ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ผ๋ก ๋์ ์๊ฐ์ ๋๋ค.
์ด๊ฒ์ด ์๋ํ๋์ง ์๋์ง? ๋๋ฅผ ์ํด ์๋ํ๋ ์ ์ผํ ํํฌ๋ '์ ์ฅ'์ด๊ณ ๋๋จธ์ง๋ ์์ ํ ๋ฌด์๋ฉ๋๋ค. ์ ๋ 4.0.6์์ ์คํ ์ค์ ๋๋ค. ๊ฐ์ฌ ํด์
@agjs ๋ ์ฝ๋ ์์ ๋ฅผ ์ ๊ณตํ์ญ์์ค.
UserController.prototype.updateAvatar = function (req, res) {
return new Promise(function (resolve, reject) {
CompanyDetails.update({
_author: req.user._id
}, {
avatarPath: req.files.file
}, function (error, updated) {
if (error) {
reject(error);
} else {
resolve(updated);
}
});
}).then(function (resolved) {
res.sendStatus(204).send(updated);
}).catch(function (error) {
next(error);
})
};
CompanyAvatarSchema.pre('update', function (next) {
console.log('pre save');
let VirtualModel = this,
parent = this.ownerDocument(),
PATH = path.normalize('./public/images/uploads/avatars/' + parent._id);
mkdirp(PATH, function (error) {
if (error) {
next(error);
} else {
fs.rename(VirtualModel.path, path.join(PATH, VirtualModel.name), function (error2) {
if (error2) {
next(error2);
} else {
next();
}
});
}
});
});
model.create ๋ฐ pre-save๋ฅผ ์ฌ์ฉํ์ฌ ๋ค๋ฅธ ๋ชจ๋ธ์ ๋ํ ๋ ๋ค๋ฅธ ์ฌ์ ํํฌ๊ฐ ์์ผ๋ฉฐ ์ ์์ ์ผ๋ก ์๋ํฉ๋๋ค.
์ ๋ฐ์ดํธ๊ฐ ๋ฌด์์ด๋ ๊ฐ์, ๊ทธ๊ฒ์ ๋จ์ํ ํํฌ๋ฅผ ๋ณด์ง ๋ชปํ๊ณ console.log์กฐ์ฐจํ์ง ์์ต๋๋ค. findOneAndUpdate ๋ฑ๋ ์๋ํ์ง๋ง ์ค์ ๋ก ์๋ํ์ง ์์ต๋๋ค ... ์๊ธด ๊ฒ์์ด ์ ์ฒด ์ค๋ ๋๋ฅผ ์ดํด๋ณด๊ณ ํ์์ ๊ฐ์ด ๊ณต์ ๋ฌธ์๋ฅผ ํ์ธํ๋๋ฐ ๊ฑฐ๊ธฐ์์๋ ์๋ํ๋ค๊ณ ์ฃผ์ฅํฉ๋๋ค.
์ ์ฝ๋์์ CompanyAvatarSchema์ CompanyDetails์ ๊ด๊ณ๋ ๋ฌด์์ ๋๊น?
ํ์ฌ ์ธ๋ถ ์ ๋ณด์๋ ํ์ ๋ฌธ์๋ก CompanyAvatar.schema๊ฐ ์์ต๋๋ค.
avatarPath: {
type: [CompanyAvatar.schema],
required: true
}
๋ํ ์ฌ์ ํํฌ๋ฟ๋ง ์๋๋ผ ์ ํจ์ฑ ๊ฒ์ฌ๋ ์์ ํ ๋ฌด์๋ฉ๋๋ค. ์ด ํ์ ๋ฌธ์๋ ์ฑ์์ง์ง๋ง ์ ํจ์ฑ ๊ฒ์ฌ์ ์ฌ์ ํํฌ๋ฅผ ๋ชจ๋ ๋ฌด์ํฉ๋๋ค. ๋๋ ๋ชจ๋ ๊ฒ์ ๊ตฌ๊ธ๋ง ํ๊ณ ์ด๊ฒ๋ ์๋ํ์ง๋ง ์๋ฌด ๊ฒ๋ ์๋ํ์ง ์์ต๋๋ค. ํ ์คํธ ๋ณ๊ฒฝ์ ์ํด new aka var parent = new Parent()๋ก ๋ชจ๋ธ์ ์์ฑํ๊ณ ํธ์ถํ๊ธฐ ์ํด ์ฟผ๋ฆฌ๋ฅผ ๋ณ๊ฒฝํ๋ฉด ์๋ํฉ๋๋ค.
CompanyDetails.update()
๋ฅผ ํธ์ถํ๊ณ ์์ง๋ง ์ฌ์ ํํฌ๋ ๋ณ๋์ ์คํค๋ง์ ์ ์๋์ด ์์ต๋๋ค. ์ฟผ๋ฆฌ ๋ฏธ๋ค์จ์ด๋ ์ค์ฒฉ ์คํค๋ง์ pre('update')
atm์ ์คํํ์ง ์์ต๋๋ค.
๋ํ '๊ฒ์ฆ์ด ์์ ํ ๋ฌด์๋จ' ๊ฒฝ์ฐ์ ๋ํ ๋ณด๋ค ์ฒ ์ ํ ์ฝ๋ ์์ ๋ ์ ๊ณตํ์ญ์์ค.
๋ค์์ ํ๋กํ(์๋ฐํ ์ฌ์ง)์ ์ ๋ฐ์ดํธํ๋ ์ฌ์ฉ์๋ฅผ ์ํ ์ ํจ์ฑ ๊ฒ์ฌ ๋ฐ ์ฌ์ ์ฐ๊ฒฐ์ ์ํด ๋ง๋ ํ์ฌ ์๋ฐํ ์คํค๋ง์ ๋๋ค.
'use strict';
let mongoose = require('mongoose'),
mkdirp = require('mkdirp'),
fs = require('fs'),
path = require('path'),
Schema = mongoose.Schema;
let CompanyAvatarSchema = new Schema({
name: String,
width: Number,
height: Number,
size: Number,
type: String
});
CompanyAvatarSchema.path('type').validate(function (type) {
return /^image\//.test(type);
}, 'Image type not allowed!');
CompanyAvatarSchema.path('size').validate(function (size) {
return size < 5;
}, 'Image too big!');
CompanyAvatarSchema.virtual('path').set(function (path) {
return this._path = path;
}).get(function () {
return this._path;
});
CompanyAvatarSchema.virtual('public_path').get(function () {
var parent = this.ownerDocument();
var PATH = path.normalize('images/uploads/avatars/' + parent._id);
if (this.name) {
return path.join(PATH, this.name);
}
});
CompanyAvatarSchema.set('toJSON', {
getters: true
});
CompanyAvatarSchema.pre('findOneAndUpdate', function (next) {
console.log('pre save');
let VirtualModel = this,
parent = this.ownerDocument(),
PATH = path.normalize('./public/images/uploads/avatars/' + parent._id);
mkdirp(PATH, function (error) {
if (error) {
next(error);
} else {
fs.rename(VirtualModel.path, path.join(PATH, VirtualModel.name), function (error2) {
if (error2) {
next(error2);
} else {
next();
}
});
}
});
});
let runValidatorsPlugin = function (schema, options) {
schema.pre('findOneAndUpdate', function (next) {
this.options.runValidators = true;
next();
});
};
CompanyAvatarSchema.plugin(runValidatorsPlugin);
let CompanyAvatar = mongoose.model('CompanyAvatar', CompanyAvatarSchema);
module.exports = CompanyAvatar;
๋ค์์ company_avatar๊ฐ ํ์ ๋ฌธ์์ธ company_details ์คํค๋ง์ ๋๋ค.
let CompanyDetailsSchema = new mongoose.Schema({
_author: [{
type: Schema.Types.ObjectId,
ref: 'CompanyAccount'
}],
company_name: {
type: String,
es_indexed: true,
es_boost: 2.0
},
contact_email: {
type: String,
es_indexed: true
},
website: {
type: String,
es_indexed: true
},
country: {
type: String,
es_indexed: true
},
industry: {
type: String,
es_indexed: true
},
address: {
type: String,
es_indexed: true
},
about: {
type: String,
es_indexed: true
},
avatarPath: {
type: [CompanyAvatar.schema],
}
});
๊ทธ๋ฆฌ๊ณ ์ด ์ ๋ฐ์ดํธ๊ฐ ์ํ๋๊ธฐ ์ ์ ๊ฒ์ฆ/์ฐ๊ฒฐ๋์ด์ผ ํ๋ ์ ๋ฐ์ดํธ ํ๋กํ ์ปจํธ๋กค๋ฌ์ avatarPath๊ฐ ์์ต๋๋ค.
UserController.prototype.updateAvatar = function (req, res, next) {
let updates = {
$set: {
avatarPath: req.files.file
}
};
return new Promise(function (resolve, reject) {
CompanyDetails.findOneAndUpdate({
_author: req.user._id
}, updates, function (error) {
if (error) {
reject(error);
} else {
resolve('done');
}
});
}).then(function () {
res.sendStatus(204);
}).catch(function (error) {
next(error);
});
};
๊ธฐ๋ณธ์ ์ผ๋ก ๋ด mongodb๋ req.files.file์ ํ๋๋ก ์ฑ์์ง์ง๋ง ๊ทธ ์ธ์๋ ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ๋ฌด์๋๊ณ ํํฌ๊ฐ ์๋ํ์ง ์์ต๋๋ค.
๋ฌธ์ ๋ pre('findOneAndUpdate') ๋ฏธ๋ค์จ์ด๊ฐ ์ค์ฒฉ๋ ์คํค๋ง์ ์ ์๋์ด ์๋ค๋ ๊ฒ์
๋๋ค. ํ์ฌ mongoose๋ ์ต์์ ์คํค๋ง์ ๋ํ ์ฟผ๋ฆฌ ๋ฏธ๋ค์จ์ด๋ง ์คํํ๋ฏ๋ก CompanyDetailsSchema
์ ์ ์๋ ๋ฏธ๋ค์จ์ด๋ ๋ค์ ์คํ๋ฉ๋๋ค. #3125
+1
@vkarpov15 ์๊ฒ ์ต๋๋ค
TodoSchema.pre('findOneAndUpdate', function() {
this.findOneAndUpdate({}, { updatedAt: Date.now() });
});
์์ฑ์ ํ๋ ๊ฐ์ผ๋ก ์ค์ ํ๊ธฐ ์ํด ์๋ํ์ง๋ง ์ํธ๋ฅผ ํด์ํ๋ ๋ฑ ์์ฑ์ ์ด๋ป๊ฒ _์ฝ๊ณ ๋ณ๊ฒฝ_ํฉ๋๊น?
TodoSchema.pre('findOneAndUpdate', function() {
this.findOneAndUpdate({}, { password: hashPassword(.....?....) });
});
์ด๊ฒฌ์๋ ์ฌ๋? ์ด๊ฒ์ ๊ฝค ์ผ๋ฐ์ ์ธ ์ฌ์ฉ ์ฌ๋ก์ ๋๋ค. ๋ง์ฃ ? ์๋๋ฉด ์ฌ๋๋ค์ด ๋ณดํต find() ํ๊ณ save() ๋ฅผ ๋ถ๋ฆฌํฉ๋๊น?
TodoSchema.pre('findOneAndUpdate', function() {
this.findOneAndUpdate({}, { password: hashPassword(this.getUpdate().$set.password) });
});
@willemmulder๊ฐ ์๋ํด์ผ ํฉ๋๋ค.
@vkarpov15 ์๋ฒฝํฉ๋๋ค, ๊ฐ์ฌํฉ๋๋ค! ์ค๋ ๋ฐค์ ์๋ํด ๋ณด๊ฒ ์ต๋๋ค. pre('update')
์์๋ ์๋ํด์ผ ํ์ง ์๋์?
@ vkarpov15 ๊ทธ๋์ ๋๋ ๊ทธ๊ฒ์ ์ฌ์ฉํ์ฌ ์๋ํ์ต๋๋ค.
schema.pre('update', function(next) {
this.update({}, { $set : { password: bcrypt.hashSync(this.getUpdate().$set.password) } });
next();
});
๋ด๊ฐ console.log this
ํ๋ฉด
_update:
{ '$set':
{ password: '$2a$10$CjLYwXFtx0I94Ij0SImk0O32cyQwsShKnWh1248BpYsJLIHh7jb66',
postalAddress: [Object],
permissions: [Object],
firstName: 'Willem',
lastName: 'Mulder',
email: '...@...',
_id: 55ed4e8b6de4ff183c1f98e8 } },
๊ด์ฐฎ์ ๋ณด์ด์ง๋ง(์ง์ ๊ทธ ์์ฑ์ ์ค์ ํ๋ ค๊ณ ์๋ํ๊ธฐ๋ ํ์ง๋ง) ๊ฒฐ๊ตญ์๋ ์ค์ ๋ก ํด์๋ ๊ฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ์ง ์๊ณ ๋จ์ํ '์์' ๊ฐ์ ๊ธฐ๋กํฉ๋๋ค. ๋ด๊ฐ ์๋ํ ์ ์๋ ๊ฒ์ด ์์ต๋๊น?
์ด์ํ๋ค. require('mongoose').set('debug', true);
๋ก mongoose ๋๋ฒ๊ทธ ๋ชจ๋๋ฅผ ํ์ฑํํ๊ณ db๋ก ์ ์ก๋๋ ์ฟผ๋ฆฌ๊ฐ ๋ฌด์์ธ์ง ํ์ธํ ์ ์์ต๋๋ค.
์ ์ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ๋ฐฉ๊ธ ๊ทธ๋ ๊ฒํ์ต๋๋ค :
๋๋ ์ด๊ฒ์ ์คํํ๋ค:
schema.pre('update', function(next) {
this.update({}, { password: bcrypt.hashSync(this.getUpdate().$set.password) } );
console.log(this.getUpdate());
next();
});
console.log์ ๋ํด ์ด๊ฒ์ ๋ฐํํฉ๋๋ค.
{ '$set':
{ password: '$2a$10$I1oXet30Cl5RUcVMxm3GEOeTFOLFmPWaQvXbr6Z5368zbfpA8nFEK',
postalAddress: { street: '', houseNumber: '', zipCode: '', city: '', country: '' },
permissions: [ '' ],
__v: 0,
lastName: '',
firstName: '',
email: '[email protected]',
_id: 563b0410bd07ce2030eda26d } }
๊ทธ๋ฆฌ๊ณ ์ด๊ฒ์ ๋ชฝ๊ตฌ์ค ๋๋ฒ๊ทธ๋ฅผ ์ํ ๊ฒ์ ๋๋ค.
Mongoose: users.update({ _id: ObjectId("563b0410bd07ce2030eda26d") }) { '$set': { password: 'test', postalAddress: { street: '', houseNumber: '', zipCode: '', city: '', country: '' }, permissions: [ '\u001b[32m\'\'\u001b[39m' ], __v: 0, lastName: '', firstName: '', email: '[email protected]', _id: ObjectId("563b0410bd07ce2030eda26d") } } { overwrite: false, strict: true, multi: false, upsert: false, safe: true }
Mongoose: users.findOne({ _id: ObjectId("563b0410bd07ce2030eda26d") }) { fields: { password: 0 } }
์ด๋ค ๋จ์?
ํ์คํ์ง ์์ต๋๋ค. ์ถ์ ํ ์ ๋ฌธ์ ๋ฅผ ์ด์์ต๋๋ค.
@vkarpov15 ๊ฐ์ฌํฉ๋๋ค. ๋ค๋ฅธ ๋ฌธ์ ๋ฅผ ์ถ์ ํ๊ฒ ์ต๋๋ค.
@vkarpov15 ์ฌ์ ํํฌ์์ ์งํ ์ค์ธ ์ฟผ๋ฆฌ์ ๋ํ ์ต์ ์ ์ค์ ํ๋ ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
finishSchema.pre('findOneAndUpdate', function (next) {
this.setOptions({
new: true,
runValidators: true
});
this.update({}, {
lastEdited: Date.now()
});
next();
});
๊ทธ๋ฌ๋ ๋ฌธ์ http://mongoosejs.com/docs/api.html#query_Query -setOptions์๋ ์ด๋ฌํ ์ต์ ์ด ์ธ๊ธ๋์ด ์์ง ์์ต๋๋ค. ์ด๊ฒ์ด ํดํน๋ ์๋ฃจ์ ์ผ๋ก ๊ฐ์ฃผ๋๋ค๋ฉด ๋ฌด์์ด ๋ ์ ์ ํ ๊น์?
๊ทธ๊ฒ์ ๋ฌธ์์ ๋ฌธ์ ์ ๋๋ค. ๊ทํ๊ฐ ์ค๋ช ํ๋ ์ฝ๋๋ ์ธ๋ป๋ณด๊ธฐ์ ์๋ํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ ๋๋ค.
๊ทธ๊ฒ์ ๋ํด ๋ณ๋์ ๋ฌธ์ ๋ฅผ ์ด โโ์ ์์ต๋๊น?
@vkarpov15 ์, ์๋ํฉ๋๋ค. ๋ด๊ฐ ์ถฉ๋ถํ ๋ช ํํ์ง ์์๋ค๊ณ ์๊ฐํฉ๋๋ค.
setOptions
๋ new
๋ฐ runValidators
๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ ์ฉํฉ๋๋ค. ์ ๋ setOptions
๋ฅผ ํตํด ํด๋น ์ต์
์ ์ค์ ํ๋ ๊ฒ์ด this.options
๋ณด๋ค ์ ํธ๋์ด์ผ ํ๋์ง ๋ฌป๊ณ ์์์ต๋๋ค.
setOptions()
๊ฐ ์ ํธ๋๋ IMO์ด์ง๋ง ๋ ๋ค ์๋ํด์ผ ํฉ๋๋ค. ์๋๋ฉด ๊ทธ๋ฅ ํ ์ ์์ต๋๋ค
this.update({}, { lastEdited: Date.now() }, { new: true, runValidators: true });
schema.pre('update', function(next) {
this.update({}, { $set : { password: bcrypt.hashSync(this.getUpdate().$set.password) } });
next();
});
์ด๊ฒ์ ๋ชจ๋ update() ํธ์ถ์์ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ฐ์ดํธํฉ๋๋ค. ๊ทธ๋์ ๋ค๋ฅธ ์์ฑ, ์ฆ ์ด๋ฆ์ด๋ ๋์ด์ ๊ฐ์ ๋ณ๊ฒฝํ๋ฉด ๋น๋ฐ๋ฒํธ๋ ์ ๋ฐ์ดํธ๋๋๋ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค!?
@nlonguit ๊ทธ๋ด ๊ฒ ๊ฐ์์. ๊ทธ๋ฌ๋ ์
๋ฐ์ดํธ๋ this
ํ๋๋ฅผ ํตํด ์ก์ธ์คํ ์ ์์ผ๋ฉฐ ๋ค์๊ณผ ๊ฐ์ด ํ ์ ์์ต๋๋ค.
if (this._fields.password) { // <- I'm sure about this one, check in debugger the properties of this
this.update({}, { $set : { password: bcrypt.hashSync(this.getUpdate().$set.password) } });
}
if (this._update.$set.password)
{
this.update({},
{ $set:
{ password: bcrypt.hashSync(this.getUpdate().$set.password)}
});
}
์ด ์ฝ๋๋ ์ ์๊ฒ ์ ๋ง์ต๋๋ค. @akoskm ๊ฐ์ฌํฉ๋๋ค
findByIdAndUpdate์ ๋ํ ์ฌ์ ํํฌ๋ ์ถ๊ฐํ ์ ์๋์ง ๊ถ๊ธํฉ๋๋ค. ๋ ๊ฐ์ ํํฌ๋ฅผ ๋ชจ๋ ์ฌ์ฉํ ์ ์์ผ๋ฉด ์ข์ ๊ฒ์ ๋๋ค.
์ด ๋ฐฉ๋ฒ์ผ๋ก ํ๊ณ ์๋ ์ค์ ๋๋ค. findById๋ฅผ ์ ๋ ฅํ ๋ค์ ํ๋๋ฅผ ์ ๋ฐ์ดํธํ์ง ์๊ณ ์ ์ฅํ ๋ค์ findByIdAndUpdate ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ธ์.
dbModel.findById(barId, function (err, bar) {
if (bar) {
bar.save(function (err) {
if (err) throw err;
});
}
});
dbModel.findByIdAndUpdate(barId, {$set:req.body}, function (err, bar) {
if (err) throw err;
res.send('Updated');
});`
๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ ๊ฐ๋๋ก ์์ฑ์ ์ค์ ํ๋ ค๊ณ ํฉ๋๋ค.
schema.post('findOneAndUpdate', function(result) {
console.log(result.comments.length);
this.findOneAndUpdate({}, { totalNumberOfComments: result.comments.length });
});
์ฟผ๋ฆฌ๊ฐ totalNumberOfComments๋ฅผ ์ค์ ํ์ง ์๊ณ ํ๋๊ฐ 0์ผ๋ก ์ ์ง๋์ง๋ง(์คํค๋ง๊ฐ ๊ธฐ๋ณธ๊ฐ: 0์ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์) ์ฌ๋ฐ๋ฅธ ๊ธธ์ด๊ฐ ๊ธฐ๋ก๋ฉ๋๋ค.
ํํฌ์ ๋์ console.log(this)
๊ฐ ์์ ๋ query
์ ๋ค์์ด ํฌํจ๋์ด ์์์ ์ ์ ์์ต๋๋ค.
_update: { '$push': { comments: [Object] }, totalNumberOfComments: 27 }
๋๋ฒ๊ทธ ๋ชจ๋๋ฅผ ์ผค ๋ ์ฟผ๋ฆฌ๊ฐ Mongoose์ ์ํด ๊ธฐ๋ก๋์ง ์์ต๋๋ค.
๋ด๊ฐ ์๋ชปํ๊ณ ์๋ ๊ฒ์ด ์์ต๋๊น, ์๋๋ฉด ์ด๊ฒ์ด ๋ฒ๊ทธ์ ๋๊น?
@zilions this.findOneAndUpdate({}, { totalNumberOfComments: result.comments.length }).exec();
๋ ์ค์ ๋ก ์ฟผ๋ฆฌ๋ฅผ ์คํํด์ผ ํฉ๋๋ค. :) ํฌ์คํธ ์ ์ฅ ํํฌ๊ฐ ๋ค๋ฅธ ์ฌํ ์ ์ฅ ํํฌ๋ฅผ ํธ๋ฆฌ๊ฑฐํ๊ธฐ ๋๋ฌธ์ ๊ฑฐ๊ธฐ์์ ๋ฌดํ ์ฌ๊ท๋ฅผ ์ป๊ฒ ๋ ๊ฒ์
๋๋ค.
@vkarpov15 ์ ๊ทธ๋ ๊ตฐ์! ๊ทธ๋ฐ ๋ค์ ๋์ this.update({} {....}).exec()
๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. :)
์ง๋ฌธํ์ง๋ง ์ด๊ฒ์ ์ฌ์ฉํ ๋ findOneAndUpdate
์ ์๋ ์
๋ฐ์ดํธ๋ ์ํํ์ง๋ง totalNumberOfComments
ํ๋๋ฅผ ์๋ฒฝํ๊ฒ ์ค์ ํฉ๋๋ค.
์๋ฅผ ๋ค์ด:
Post.findOneAndUpdate({_id: fj394hri3hfj}, {$push: {comments: myNewComment}})
๋ค์ ํํฌ๋ฅผ ํธ๋ฆฌ๊ฑฐํฉ๋๋ค.
schema.post('findOneAndUpdate', function(result) {
this.update({}, {
totalNumberOfComments: result.comments.length
}).exec();
}));
ํ์ง๋ง ํํฌ๋ $push
์์ comments
$๋ก ๋ค์ myNewComment
์ด๋ฏ๋ก ์ค๋ณต ํญ๋ชฉ์ด ์์ฑ๋ฉ๋๋ค.
๊ธฐ์ ์ ์ผ๋ก ๋์ผํ ์ฟผ๋ฆฌ๋ฅผ ์คํํ๊ณ ์๊ธฐ ๋๋ฌธ์ -
schema.post('findOneAndUpdate', function(result) {
this.update({}, {
totalNumberOfComments: result.comments.length
}).exec();
}));
๋ณธ์ง์ ์ผ๋ก ๋์ผํ๋ค
var query = Post.findOneAndUpdate({_id: fj394hri3hfj}, {$push: {comments: myNewComment}});
query.update({}, {
totalNumberOfComments: result.comments.length
}).exec();
query.findOneAndUpdate().exec();
์ฒ์๋ถํฐ ์ ์ฟผ๋ฆฌ๋ฅผ ๋ง๋ค๋ ค๋ฉด ๋ค์์ ์ํํ์ญ์์ค.
schema.post('findOneAndUpdate', function(result) {
this.model.update({}, { // <--- `this.model` gives you access to the `Post` model
totalNumberOfComments: result.comments.length
}).exec();
}));
์ฌ์ ์ ์ฅ ํํฌ๋ฅผ ๋ง์ง์ง ๋ง์ญ์์ค.
router.put('/:id', jsonParser, function(req, res, next) {
currentCollection.findByIdAndUpdate(req.params.id, req.body, function (err, item) {
if (err) {
res.status(404);
return res.json({'error': 'Server Error', 'trace': err});
}
item.save(); // <=== this is were you save your data again which triggers the pre hook :)
res.status(200);
return res.json({'message': 'Saved successfully'});
});
});
๋ชจ๋ธ์ ์ ์ํ๊ณ pre
ํํฌ๋ฅผ ์ ์ํ๋ ์์๊ฐ ์ค์ํ๋ค๋ ๊ฒ์ ์์์ต๋๋ค. ๋ด๊ฐ ๋ณด์ฌ์ค ์ ์๋๋ก:
์๋ํ์ง ์์:
// Create Model
let model = Database.Connection.model(`UserModel`, this._schema, `users`);
// Attach Pre Hook
this._schema.pre(`findOneAndUpdate`, function(next) {
console.log('pre update');
return next();
});
์๋ํฉ๋๊น:
// Attach Pre Hook
this._schema.pre(`findOneAndUpdate`, function(next) {
console.log('pre update');
return next();
});
// Create Model
let model = Database.Connection.model(`UserModel`, this._schema, `users`);
์ด๊ฒ์ด ๋๊ตฌ์๊ฒ๋ ๋์์ด๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค!
๋ฐฉ๊ธ @nicky-lenaers์ ๊ฐ์ ๊ฒ์ ๋ฐ๊ฒฌํ์ต๋๋ค.
'safe'
์์๋ ์ ์๋ํฉ๋๋ค. 'delete'
. ๋ฑ. ๋ชจ๋ธ์ด ์ ์๋ ํ ํํฌ๋ฅผ ์ ์ํ๋ ๊ฒฝ์ฐ.
๋ชจ๋ธ์ด ์ ์๋ ํ 'findOneAndUpdate'
ํํฌ๋ฅผ ์ ์ํ๋ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
@albert-92 ์๋์ ์ง๊ธ์ ์๋๋๋ค
์์ ์ ์ข์ํ๋ ๊ฒ์ ์ป์ผ๋ ค๊ณ ํ๋ ์ฌ๋์ ์ํด
SCHEMA.pre('validate', function(done) {
// and here use something like
this.yourNestedElement
// to change a value or maybe create a hashed character
done();
});
์ด๊ฒ์ ์๋ํด์ผํฉ๋๋ค
SCHEMA.pre('findOneAndUpdate', function(done){
this._update.yourNestedElement
done();
});
์ปฌ๋ ์ ์ ๋ฌธ์๋ฅผ ์ ๋ฐ์ดํธํ๊ธฐ ์ํ ํฌ์คํธ ํํฌ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
`module.exports = ํจ์(๋ชฝ๊ตฌ์ค) {
var mySchema = ๋ชฝ๊ตฌ์ค.์คํค๋ง({
id: { ์ ํ: ์ซ์, ์์ธ: { ๊ณ ์ : ์ฐธ } },
field1: { ์ ํ: ๋ฌธ์์ด },
field2: {์ ํ: ๋ฌธ์์ด}
}, {
์ปฌ๋ ์
: "mySchema",
๋ฒ์ ํค: ๊ฑฐ์ง
});
mySchema.post('findOneAndUpdate', function (result) {
this.model.update({}, {
field2: 'New Value'
}).exec();
});
return mySchema;
}`
mySchema.findOneAndUpdate({id: 1}, {field1: 'test'}, {new: true});
์ปฌ๋ ์
์ ํ๋๋ฅผ { id:1, field1: 'test' )๋ก ์ค์ ํ์ง๋ง {id: 1, field1: 'test', field2:'New Value'}์ฌ์ผ ํฉ๋๋ค.
๋ด๊ฐ ๋ญ ์๋ชปํ๊ณ ์๋์ง ํ์คํ์ง ์๋ค
๋ค์์ ์ํํ์ฌ findOneAndUpdate์ ๊ฒฐ๊ณผ๋ฅผ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
mySchema.post('findOneAndUpdate', function (result) {
result.field2 = 'something'
});
๋ชจ๋ธ์ ์ด๋ฏธ ์กด์ฌํ๋ ์์๋ก ๋ชจ๋ธ์ ์ ๋ฐ์ดํธํ๋ ค๊ณ ํ๋ ๊ฒ ๊ฐ์ต๋๋ค. ์๋๋ฉด ์๋ชป ์ ํํ์ ์๋ ์์ต๋๋ค. mySchema.post์์ "this"๋ฅผ ์ธ์ํด ๋ณด์ญ์์ค. ๋ํ ๊ฒ์๋ฌผ์ done() ๋๋ next()๊ฐ ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ๋๋ ๊ทธ ์ฃผ์ ์ ๋ํด ์ ์์ง ๋ชปํ์ง๋ง ์ด๊ฒ์ ์ธ์ํ๋ฉด ์ต์ํ ๋น์ ์ด ๋ฌด์์ ๋ค๋ฃจ๊ณ ์๋์ง์ ๋ํ ์์ด๋์ด๋ฅผ ์ป์ ์ ์๋ค๋ ๊ฒ์ ์๋๋ค.
์ ๋ฐ์ดํธ์ ํฌ์ธํธ๋ ๋ชจ๋ธ์ ์๋ ๊ธฐ์กด ๋ฌธ์๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ ์๋๊ฐ์?
์ด๊ฒ์ ์ฟผ๋ฆฌ ๊ฐ์ฒด์ ๋๋ค
๋ด๊ฐ ์ดํดํ๋ ํ ํฌ์คํธ ํํฌ์์ done ๋๋ next๊ฐ ํ์ํ์ง ์์ต๋๋ค.
๊ฐ์ฒด์ ๋ชจ๋ธ์ด ์๋ ์คํค๋ง์ธ this.model.update๊ฐ ์์ต๋๋ค. ์ ์๊ฐ์๋ .. ๋น์ ์ด ์ฌ์ฉํด์ผ ํ ๊ฒ์ ๋๋ค.
mySchema.post('findOneAndUpdate', function (result) {
this.model.update({}, {
$set: { field2: 'New Value'}
}).exec();
});
return mySchema;
๋ชจ๋ธ ๋ด๋ถ์์ ๋ชจ๋ธ ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ ์ฝ๊ฐ ๊ฑฐ๊พธ๋ก ๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ฃผ์ด์ง "this" ๊ฐ์ฒด์ ์ผ๋ถ๋ง ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค. findOneAndUpdate๋ฅผ ํธ์ถํ ๋ค์ ๊ทธ ์์ ๋ค๋ฅธ ๋ชจ๋ธ ํจ์๋ฅผ ํธ์ถํ๋ ๋์ findOneAndUpdate๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ๋์ ์ ์์ต๋๋ค.
๋งค๋์ ์์
var data = yourNewData;
self.findOneAndUpdate({id: something._id}, data, {safe: false, new: true})
.exec()
.then(resolve)
.catch(reject);
์์ ์์์ this._update๋ฅผ ์ฌ์ฉํ์ต๋๋ค. ์๋ํ๋ฉด ์ด๊ฒ์ด ์ด๊ฒ์์ ์ฌ์ฉ๋์ด์ผ ํ๋ ์ ๋ฐ์ดํธ ๊ฐ์ฒด์๊ธฐ ๋๋ฌธ์ ๋๋ค.
$set์ ์ฌ์ฉํด ๋ณด์์ต๋๋ค. ์ปฌ๋ ์ ์ ์๋ ๋ด ๋ฌธ์๋ ์ฌ์ ํ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค.
์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ ์ฌ์ ๋ฐ ์ฌํ ํํฌ๋ ์ด๋์์ ์ฐพ์ ์ ์์ต๋๊น?
๋ฏธ๋ค์จ์ด ๋ฌธ์ http://mongoosejs.com/docs/middleware.html
+1
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์๋ ํ์ธ์,
๋ฌธ์ ๊ฐ ์ข ๋ฃ๋์๋ค๋ ๊ฒ์ ์๊ณ ์์ง๋ง ๋ต๋ณ์ ์์ ํ ๋ง์กฑํ์ง ์์ต๋๋ค. findOneAndUpdate ๋ฐ ๊ธฐํ ์ ์ฌํ ์์ ์ ์ํ ์ฌํ ์ ์ฅ ๋๋ ์ฌํ ์ ๋ฐ์ดํธ ๋ฏธ๋ค์จ์ด๊ฐ ์์ด์ผ ํ์ง ์์ต๋๊น? ๋ฌธ์๊ฐ ๋ฐํ๋์์ผ๋ฏ๋ก ๊ด์ฐฎ์ ๋ณด์ ๋๋ค. ์ฌ์ ๋ฏธ๋ค์จ์ด ๋๋ Model.update์๋ ์ ํฉํ์ง ์์ต๋๋ค. ๋์ํฉ๋๋ค.
ํ์ฌ ์ง์ํด์ผ ํ๋ ์ผ๋ถ ์์ ์ ๋ํด ๋ธ๋ผ์ธ๋์ธ mongoosastic๊ณผ ๊ฐ์ ํ๋ฌ๊ทธ์ธ์ ๊ธฐ๋ฅ์ ํฌ๊ฒ ํฅ์์ํฌ ๊ฒ์ ๋๋ค.
๋ฏธ๋ค์จ์ด๊ฐ ์๋ค๋ฉด ํ๋ฌ๊ทธ์ธ์์ ์ผ๋ถ ์ฌํ ์ ๋ฐ์ดํธ ์์ ์ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ด๋์ด๊ฐ ์์ต๋๊น?
๊ฐ์ฌ ํด์