Javascript 猫鼬检查是否存在收集
我正在尝试向mongodb中插入800个对象,同时我也在尝试防止一次又一次地插入相同的元素。我将检查集合是否存在,不插入,否则插入800个对象Javascript 猫鼬检查是否存在收集,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在尝试向mongodb中插入800个对象,同时我也在尝试防止一次又一次地插入相同的元素。我将检查集合是否存在,不插入,否则插入800个对象 mongoose.connect('mongodb://localhost/testDB', function(err,db){ if(err){ console.log(err) } db.listCollections().toArray(function(err, collections){ console.log(co
mongoose.connect('mongodb://localhost/testDB', function(err,db){
if(err){
console.log(err)
}
db.listCollections().toArray(function(err, collections){
console.log(collections);
});
});
但控制台抛出错误并显示:
(node:24452) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: db.listCollections is not a function
(node:24452) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我现在卡住了,谢谢。client.db上提供了listCollections功能,您尝试过这个吗
mongoose.connect('mongodb://localhost/testDB', function(err, client) {
if(err) {
console.log(err)
}
client.db.listCollections().toArray(function(err, collections) {
console.log(collections);
});
});
查看是否适用于您。TypeError:无法读取未定义的对象的属性“listCollections”,如未定义连接对象db。查看是否与您的问题相关。您必须通过传递db名称从db中获取db对象。登录数据库的url if中的数据库。