정책에 대한 문서를 만들거나 업데이트하는 단일 메서드가 필요합니다. 검색하고 다른 기술을 시도 this one, 나는 내 문서에 대한 null _id와 함께 왔습니다. findByIdAndUpdate를 사용하면 비슷한 효과가 있습니다.몽구스 findOneAndUpdate Upsert _id null?
나는 컬렉션에 삽입 된 문서를 볼 수 있지만, _id 필드가 null :
exports.savePolicy = function (plcy, callback) {
console.log('priority is : ' + plcy.priority)
try {
var policy = new Policy(plcy);
var query = {_id: plcy._id}; //this may be null
var update = {
name: plcy.name || defaults.policyDefaults.name,
longDescription: plcy.longDescription || defaults.policyDefaults.longDescription,
shortDescription: plcy.shortDescription || defaults.policyDefaults.shortDescription,
priority: plcy.priority, colorHex: plcy.colorHex || defaults.policyDefaults.colorHex,
settings: plcy.settings || [],
parentPolicyId: plcy.parentPolicyId || null
}
Policy.findOneAndUpdate(query, update, {upsert: true}, function (err, data) {
callback(err, data);
});
} catch (e) {
log.error('Exception while trying to save policy: ' + e.message);
callback(e, null);
}
때이 아니 업데이 트 null이 될하지 않도록 _id을 얻기 위해 할 수있는 일이 있습니까?
혹시이 알아낼 않았다? 나는이 같은 문제를 겪고 있었다. 결국 _id를 수동으로 확인한 다음 findOneAndUpdate를 호출하여 새 레코드를 만들거나 create()를 호출합니다. – Trent