繁体   English   中英

如何不将多个数据保存到 mongodb 中的集合中?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM