db.collectionNames在Node.js中不起作用 [英] db.collectionNames doesn't work in Node.js

查看:77
本文介绍了db.collectionNames在Node.js中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查Node.js中是否存在集合.我使用db.collectionNames来获取db中的名称列表,但没有任何反应.代码:

I want to check if a collection exist in Node.js. I use the db.collectionNames to get the names list in the db but nothing happened. The code:

connectDB(DBURL).then(function(db) {
        console.log('db connect ok');

        db.collectionNames('test', function(err, collectionNames) {
            console.log('get collection names');

            if(err) console.log(err);
            else console.log(collectionNames);
        });

    }, function(err) {
        console.log(err);
    });

connectDB(DBURL)是一个Promise对象,它可以完美运行.输出:

The connectDB(DBURL) is a promise object, it works perfectly. The output:

app-0 try to connect db
app-0 db connect ok

您可以看到collectionNames中的函数没有任何输出.我不知道为什么.

You can see there's nothing output from the function in collectionNames. I have no idea why.

我可以通过db.getCollectionNames在Mongo shell中获得集合名称:

I can get the collections name in Mongo shell by db.getCollectionNames:

> db.getCollectionNames()
[ "system.indexes", "test" ]

推荐答案

您是否正在使用> 2.0版本的驱动程序?

Are you using >2.0 version of the driver?

如果是这样,您将需要使用 listCollections代替-这是从1.x版更新的更改之一

If so, you will need to use listCollections instead - this is one of the changes in the update from 1.x

类似的东西:

db.listCollections().toArray(function(err, collections){
    //collections = [{"name": "coll1"}, {"name": "coll2"}]
});

这篇关于db.collectionNames在Node.js中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆