node.js - db.getCollectionInfos 不是函数
问题描述
我正在编写一个 MERN 应用程序,并尝试使用该db.getCollectionInfos
方法获取我的 MongoDB 数据库中的所有集合名称。但是,我收到以下错误:
Error: db.getCollectionInfos is not a function
这是我的代码。数据库已连接,因为它已连接并返回文档数据以及我在我的应用程序中拥有的其他路线。
有人可以帮我弄清楚我做错了什么吗?
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true });
const db = mongoose.connection;
app.post("/fetchdatabasecollections", (req, res) => {
let regex = /someRegEx/g;
let collections = db.getCollectionInfos({ name: { $regex: regex } });
})
解决方案
getCollectionInfos
是 mongo shell 方法,不是由 nodejs 驱动程序提供的。listCollections
改为使用
const filter = { name: { $regex: regex } };
const collectionInfos = await mongoose.connection.db.listCollections(filter).toArray();
推荐阅读
- mysql - 如何正确使用“JSON_TABLE”从 MySQL8 中的 Json 数组中获取匹配的名称?
- animation - FFmpeg多个drawtext过滤器彼此相邻
- python - 从没有零的 3d 数组中取平均值
- javascript - Vimeo iframe 嵌入视频仅对其他 Vimeo iframe 自动暂停。当播放同一页面上的 YouTube iframe 时,它们不会暂停
- ios - 如何在 Objective-C 中将多个 ViewController 链接到 NavigationController
- amazon-web-services - AWS:帮助在 Elastic Beanstalk 上配置端到端加密
- javascript - React axios - TypeError:无法读取未定义的属性'post'
- javascript - 使用 AJAX、JQuery 和 HTML 显示/隐藏数据
- java - 如何用 java 2d 数组解决这个问题?
- python - 根据另一个列表排序对对象列表进行排序