我正在编写一个 MERN 应用程序,并尝试使用以下命令获取 MongoDB 数据库中的所有集合名称db.getCollectionInfos
method https://docs.mongodb.com/manual/reference/method/db.getCollectionInfos/。但是,我收到以下错误:
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
instead
const filter = { name: { $regex: regex } };
const collectionInfos = await mongoose.connection.db.listCollections(filter).toArray();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)