[英]How to not save more then one data into a collection in mongodb?
如果該集合中已經存在數據,我不想保存其他數據
這是一個簡單的實現,如果集合中已經存在文檔,如何防止寫入文檔。
Collection.find( {}, (err, docs) => { // By not giving any arguments inside "{}", you tell MongoDB to return all the documents.
if(docs) { // If there are any documents already present in the Collection, this will become true and nothing will happen.
return;
} else { // If there's no document already present in the Collection, new document will be added.
Collection.save(doc);
}
} );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.