node.js - 如何不将多个数据保存到 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);
}
} );
推荐阅读
- python-3.6 - 对寻找因子的程序感到困惑
- php - PHP 7.2 + Laravel 5.8:调用 Trait 中定义的父方法被使用此 Trait 的类中的父方法调用覆盖
- excel - 在用户窗体中使用私有子的单元格地址
- doxygen - 呼吸找不到C#接口
- python - 如何通过训练图像数据集来预测非离散值?
- python - 如何合并列表,但保持以前的列表顺序?
- reactjs - 大量自定义 Material UI DataTable (React)
- python - 调用函数与内联代码时的不同行为
- python - 无法正确安装 setup.py 以进行人脸识别
- javascript - Vuex:突变中的均方括号是什么?