Mongoose: μ €μž₯을 μœ„ν•΄ 린을 μ‚¬μš©ν•©λ‹ˆκΉŒ?

에 λ§Œλ“  2013λ…„ 10μ›” 03일  Β·  16μ½”λ©˜νŠΈ  Β·  좜처: Automattic/mongoose

μ €μž₯을 μœ„ν•΄ λ¦° μ˜΅μ…˜μ„ μ‚¬μš©ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.
save μ½œλ°±μ€ λ¦° 같은 ν˜ΈμΆœμ„ μ‚¬μš©ν•˜μ—¬ λ³€κ²½ν•˜λ €λŠ” JS 객체가 μ•„λ‹Œ λͺ½κ΅¬μŠ€ λ¬Έμ„œλ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.

쿼리에 린을 μ‚¬μš©ν•˜κ³  μ €μž₯의 콜백 ν•¨μˆ˜μ—μ„œ toObjectλ₯Ό μ‚¬μš©ν•˜λŠ” 것은 λ‚˜μ—κ²Œ ν˜Όλž€μ²˜λŸΌ λ³΄μž…λ‹ˆλ‹€.

μ–΄λ–€ 아이디어?

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

@imrefazekas , Save, Update, Create 및 RemoveλŠ” "쿼리"κ°€ μ•„λ‹ˆλ―€λ‘œ .lean() μ§€μ‹œλ¬Έμ„ κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
λ°˜ν™˜λœ 객체에 .toObject() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μŠ€ν‚€λ§ˆ 없이 κ°€μ Έμ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

p.save(function (err, returned) {
  var leanObject = returned.toObject(); // has many options: http://mongoosejs.com/docs/api.html#document_Document-toObject
  assert.equal(leanObject.schema, null);
});

λͺ¨λ“  16 λŒ“κΈ€

린을 μ‚¬μš©ν•˜μ§€ μ•ŠμœΌλ €λ©΄ λ°˜ν™˜λœ Mongoose κ°μ²΄μ—μ„œ μƒˆ 객체λ₯Ό μƒμ„±ν•˜λŠ” 것이 κ°€μž₯ μ’‹μŠ΅λ‹ˆλ‹€.

Person.findOne({
    _id: req.user.id
}, function(err, result) {
    if (err)
        return next(err);

    return (200, {
        id: result._id,
        spouse: result.spouse,
        children: result.children
    });
});

λ‚΄ κ²Œμ‹œλ¬Όμ΄ λͺ…ν™•ν•˜μ§€ μ•Šμ•˜μ„ 수 μžˆμŠ΅λ‹ˆλ‹€. μ£„μ†‘ν•©λ‹ˆλ‹€.
린을 μ‚¬μš©ν•˜κ³  μ‹Άμ§€λ§Œ μ €μž₯ μž‘μ—…μ—λ„ μ‚¬μš©ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.
"μ €μž₯" λ˜λŠ” "μ—…λ°μ΄νŠΈ"와 같은 μ €μž₯ ν•¨μˆ˜μ˜ μ½œλ°±μ€ μŠ€ν‚€λ§ˆ κ°œμ²΄μ™€ ν•¨κ»˜ λ°˜ν™˜λ˜λ©° 순수 JS 개체λ₯Ό κ°–κΈ° μœ„ν•΄ 린을 μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.
이것은 λ‚΄ λ¬Έμ œμž…λ‹ˆλ‹€. μ €μž₯ 및 μ—…λ°μ΄νŠΈμ—λ„ 린을 μ‚¬μš©ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

@imrefazekas , Save, Update, Create 및 RemoveλŠ” "쿼리"κ°€ μ•„λ‹ˆλ―€λ‘œ .lean() μ§€μ‹œλ¬Έμ„ κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
λ°˜ν™˜λœ 객체에 .toObject() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μŠ€ν‚€λ§ˆ 없이 κ°€μ Έμ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

p.save(function (err, returned) {
  var leanObject = returned.toObject(); // has many options: http://mongoosejs.com/docs/api.html#document_Document-toObject
  assert.equal(leanObject.schema, null);
});

흠흠... save() 에 λ¦° μ˜΅μ…˜μ΄ μžˆμ–΄μ•Ό ν•©λ‹ˆκΉŒ?

save()와 lean이 ν•¨κ»˜ μΌν•˜λŠ” 것을 μ–΄λ–»κ²Œ 상상할 수 μžˆμŠ΅λ‹ˆκΉŒ? 말이 μ•ˆ λ˜λŠ” AFAIK.

음, 며칠이 μ§€λ‚¬μ§€λ§Œ ν˜„μž¬λŠ” μ΄λ ‡μŠ΅λ‹ˆλ‹€.

user.save(function(err,user){

      //user is full-fledged etc
});

λ¦° μ˜΅μ…˜μ„ μ‚¬μš©ν•˜λ©΄

user.save({
  lean:true
},function(err,user){

      //user is *not* full-fledged etc
});

그렇지 μ•ŠμŠ΅λ‹ˆκΉŒ?

@ORESoftware μ•„λ‹ˆμš”. μ €μž₯ν•  콜백의 두 번째 맀개 λ³€μˆ˜κ°€ μ •ν™•νžˆ λ™μΌν•œ λ¬Έμ„œμ΄κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. MongooseλŠ” μƒˆλ‘œ μ—…λ°μ΄νŠΈλœ λ¬Έμ„œλ₯Ό λ°μ΄ν„°λ² μ΄μŠ€μ— 묻지 μ•ŠμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄,

user.save(function(err, _user){
  user === _user; // true
});

였 μ•Œμ•˜μ–΄ :) λ‚΄ λ‚˜μœ

기둝을 μœ„ν•΄ μœ„μ—μ„œ @repack이 μ œκ³΅ν•œ μ†”λ£¨μ…˜μ€ 2017년에도 μ—¬μ „νžˆ 맀λ ₯처럼 μž‘λ™ν•©λ‹ˆλ‹€.

@peterpeterparker λ‚˜λŠ” λ™μ˜ν•˜μ§€ μ•ŠμœΌλ©° 이것이 Mongoose μΈμŠ€ν„΄μŠ€ λŒ€μ‹  λͺ¨λ“  λ°μ΄ν„°λ² μ΄μŠ€ μž‘μ—…μ—μ„œ 데이터가 λ°˜ν™˜λ˜κΈ°λ₯Ό μ›ν•˜λŠ” μ‚¬μš©μžμ—κ²Œ μ—¬μ „νžˆ 문제라고 μƒκ°ν•©λ‹ˆλ‹€.

@alex-dixon μ €λŠ” save μž‘μ—…μ— λŒ€ν•œ @repack 의 μ†”λ£¨μ…˜μ΄ 2017년에도 μ—¬μ „νžˆ μœ νš¨ν•˜κ³  κΈ°λŠ₯적이라고 μ§€μ ν–ˆμŠ΅λ‹ˆλ‹€. κ·Έ 이상도 κ·Έ μ΄ν•˜λ„ μ•„λ‹™λ‹ˆλ‹€ ;)

@peterpeterparker λ§žμŠ΅λ‹ˆλ‹€. μ£„μ†‘ν•©λ‹ˆλ‹€. λ‹Ήμ‹ μ—κ²Œ 그것을 μ§€μ‹œν•˜μ§€ λ§μ•˜μ–΄μ•Ό ν–ˆλ‹€.

@alex-dixon κ±±μ •λ§ˆ

μš°λ¦¬κ°€ μ‚¬μš©ν•œλ‹€ lean() 와 Model.create() ?

lean() 와 ν•¨κ»˜ Model.create() lean() λ₯Ό μ‚¬μš©ν•  수 μ—†κΈ° λ•Œλ¬Έμ— μ ˆλŒ€λ‘œ :p

μ•Œμ•˜μ–΄μš” :)

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰