2017-12-07 2 views
0

전체 평균 스택 작업. 내 mongodb 문서 ID # 1의 "notes"배열에 데이터를 쓰려고합니다.angular/express api/mongodb 업데이트 문제

몽고 문서 :

> db.contacts.find().pretty() 
{ 
"_id" : ObjectId("5a294af85e96746421bf35f1"), 
"id" : 1, 
"type" : "dealer", 
"name" : "ken yoder", 
"company" : "kens sales", 
"phone" : "817-403-9767", 
"notes" : [ 
    { 
     "date" : "Thu Dec 07 2017 08:15:37 GMT-0600 (CST)", 
     "by" : "@me", 
     "note" : "this is a note" 
    }, 
    { 
     "date" : "Thu Dec 07 2017 08:16:31 GMT-0600 (CST)", 
     "by" : "@donny", 
     "note" : "bla bla bla mumford and sons" 
    }, 
    { 
     "date" : "Thu Dec 07 2017 08:34:03 GMT-0600 (CST)", 
     "by" : "@ken", 
     "note" : "test with another note" 
    }, 
    { 
     "date" : "Thu Dec 07 2017 08:34:29 GMT-0600 (CST)", 
     "by" : "@ken", 
     "note" : "test with another notey note note" 
    } 
], 
"setNotes" : { 
    "date" : "Thu Dec 07 2017 10:52:09 GMT-0600 (CST)", 
    "by" : "@kale", 
    "note" : "hhsdihufiudhsiuhdshuifds" 
} 
} 

특급 코드 :이 앉아 내가이 코드를 실행하면

app.get('/newNote/:noteFor/:noteCount/:noteBy/:note/', function (req, res) { 
     var setNotes = "notes."+req.params.noteCount; 
     db.collection('contacts').update({ id:req.params.noteFor }, { $set: { setNotes : {date: Date(), by: req.params.noteBy, note: req.params.note} }}, function(err, res) { 
       if (err) throw err; 
       console.log("notefor: "+req.params.noteFor+" noteCount: "+setNotes+" noteBy: "+req.params.noteBy+" note: "+req.params.note); 
       console.log(res.result.nModified + " for " + req.params.noteFor + " updated"); 
     }); 
}); 

, 난 아무것도없이 삽입, 아니 오류를 얻을. - 업데이트 ID를 "req.params.id"에서 "1"로 변경하면 삽입이되지만 ...- 내 "setNotes"변수 ("notes.4"를 출력 함)를 사용하는 대신 $ 세트 식별자, 그것은 "setNotes"라는 새로운 개체를 만들었습니다.

API URL: /newNote/1/4/@kale/this is a note test 
+0

오 와우, 바보 같은 ... 괜찮아. 감사합니다. "setNotes"var은 어떨까요? 그런 식으로 $ 집합에 이름과 개수를 전달할 수 있습니까? –

+0

이 코드를 업데이트했습니다. id : req.params.noteFor 아직 삽입되지 않았습니다. –

+0

app.get ('/ newNote/: noteFor/: noteCount/: noteBy/: 참고 /', function (req, res) { var setNotes = "메모"+ req.params.noteCount; db.collection update ({id : req.params.noteFor}, {$ set : {setNotes : {date : Date(), by : req.params.noteBy, note : req.params.note}}} noteCount : "+ setNotes +"noteBy : "+ req.params.noteBy +"note : "(noter :"rege.params.noteFor " + req.params.note); console.log (res.result.nModified + "for"+ req.params.noteFor + "updated"); }); }); –

답변

0

이것은 단지 notes 배열에 새 문서마다 추가됩니다 :

db.collection('contacts').update({ id:req.params.noteFor }, { 
      $push: { 
        notes : {date: Date(), by: req.params.noteBy, note: req.params.note} 
       } 
      }, function(err, res) { 
     //code 
} 
+0

예! 너는 천재 야. 고마워. –

0

첫 번째 읽기 express api 기본, http://expressjs.com/en/4x/api.html. 질문에서 req.params.id에 id가 아니라 req.params에 4 개의 키 (noteFor, noteCount, noteBy 및 note)가 포함되어 있습니다. 또한 쿼리 통지의 결과에 의해 기능을 varible 고해상도를 오버라이드 (ERR, 고해상도) {.........

db.collection('contacts').update(
    { id:req.params.id }, 
    { $set: 
     { setNotes : {date: Date(), by: req.params.noteBy, note: req.params.note} } 
    }, 
    { 
     // if noting found then stop create new document with values of $set 
     upsert:false 
     // result should be updated document 
     new:true 
    }, 
    function(err, result) { 
     console.log(err, result) 
    }) 
+0

이 코드를 업데이트했습니다. id : req.params.noteFor 아직 삽입되지 않았습니다. –

+0

mongoose.connect (process.env.MONGO_URI); mongoose.set ('debug', true); 콘솔에서 디버그 쿼리를 게시하십시오. – shivshankar

+0

그게 무슨 뜻입니까? –