전체 평균 스택 작업. 내 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
오 와우, 바보 같은 ... 괜찮아. 감사합니다. "setNotes"var은 어떨까요? 그런 식으로 $ 집합에 이름과 개수를 전달할 수 있습니까? –
이 코드를 업데이트했습니다. id : req.params.noteFor 아직 삽입되지 않았습니다. –
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"); }); }); –