2013-10-09 7 views
0

.select() 필드가 제거 된 쿼리에서 검색 한 몽구스 문서의 .save()이 불완전한 문서를 mongo의 문서 표현에 쓰게합니까?필드를 제거한 몽구스 문서 저장

그렇다면 필드 선택을 사용하지 않거나 .update()을 별도로 발행해야합니까?

Posts 
    .findById(someId) 
    .select('-body') 
    .exec(function(err, post){ 
    post.edited = Date.now(); 
    post.save(function(err){ 
     // will `post` still have the body field if I query for it from the database again? 
    }) 
    }) 
+0

4.1.6에서 ("_ id")를 선택한 다음 즉시 저장()하면 큰 문서에서 무작위로 데이터 손실이 발생합니다. –

답변

7

음, 그냥 :)

// uses streamline.js 
var mongoose = require('mongoose'); 
var client = mongoose.connect('mongodb://localhost/test'); 

var Doc = mongoose.model('Doc', new mongoose.Schema({ 
    name : String, 
    body : String 
})); 

var doc  = new Doc({ name : 'foo', body : 'this is the body' }).save(_); 
var result = Doc.findById(doc._id).select('-body').exec(_); 

console.log('R#1', result); 

doc.name = 'new name'; 
var newdoc = doc.save(_); 

var result2 = Doc.findById(newdoc._id).exec(_); 

console.log('R#2', result2); 

이 인쇄를 시도 :

R#1 { name: 'foo', _id: 525506fb23c4904b61000001, __v: 0 } 
R#2 { __v: 0, 
    _id: 525506fb23c4904b61000001, 
    body: 'this is the body', 
    name: 'new name' } 

그래서 body 속성이 여전히 존재합니다.

기존 문서의 경우 .save()이 실제로 .update()을 내부적으로 실행하는 이유는 무엇입니까?