我試圖在NodeJS中使用一些打字稿代碼在mongodb文檔中插入/更新字符串數組。 下面的代碼是打字稿,但我想JS開發者將得到它的W/O型的任何問題:MongoDB Upsert添加到陣列
export function addEvents(entityId: string,
events: string[] ,
callback:() => void) {
db.collection('events', function(error, eventCollection) {
if(error) {
console.error(error); return;
}
eventCollection.update({ _id: entityId }, { "$pushAll ":
{ events: events }},
function(error, result) {
if(error) {
console.error(error); return;
}
callback();
});
});
}
文檔具有以下結構:
{
_id : string
events : ["array","of","strings"]
}
我只是想在追加一個字符串數組現有數組的結尾用於特定的_id。
我不太得到,如果我要使用更新,保存,$推,$ pushall等
有人能解釋一下嗎?