Sco*_*ott 6 mongoose mongodb node.js express
我正在编写一个 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 } });
})
Run Code Online (Sandbox Code Playgroud)
小智 7
getCollectionInfos是mongo shell方法,不是nodejs驱动提供的。改用listCollections
const filter = { name: { $regex: regex } };
const collectionInfos = await mongoose.connection.db.listCollections(filter).toArray();
Run Code Online (Sandbox Code Playgroud)
小智 -2
试试这个兄弟..
var search_something = 'abcd';
var collections = db.getCollection('YOUR_TABLE').find( {
name: new RegExp( '^' + search_something ),
} );Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1295 次 |
| 最近记录: |