如何检查Mongodb native nodejs驱动程序中是否存在集合?

Nas*_*ade 36 mongodb node.js node-mongodb-native

我需要检查某个数据库中是否存在集合,如果不存在则创建它.我知道

db.createCollection(collName, {strict:true}, function(error, collection))
Run Code Online (Sandbox Code Playgroud)

collName在创建之前检查是否存在集合并设置error对象.但我需要一个独立的功能来检查.

Joh*_*yHK 34

collectionNames机驱动程序Db对象的方法接受可选的集合名称过滤器作为第一个参数,以便您检查集合的存在:

db.collectionNames(collName, function(err, names) {
    console.log('Exists: ', names.length > 0);
});
Run Code Online (Sandbox Code Playgroud)

在2.x版本的MongoDB本机驱动程序中,collectionNames已被替换为listCollections接受过滤器并返回游标,因此您将执行以下操作:

db.listCollections({name: collName})
    .next(function(err, collinfo) {
        if (collinfo) {
            // The collection exists
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • 还有一行`db.listCollections({ name: colName }).hasNext()` (8认同)
  • 这应该是公认的答案,因为它是实际处理node.js的唯一答案(就像OP要求的那样). (7认同)

Der*_*ick 14

在MongoDB 3.0及更高版本中,您必须运行命令以列出数据库中的所有集合:

use test;
db.runCommand( { listCollections: 1 } );
Run Code Online (Sandbox Code Playgroud)

虽然system.namespaces在使用默认存储引擎(MMAPv1)时查询仍然有效,但不能保证其他引擎(例如WiredTiger)可以使用.

在MongoDB 3.0之前,您需要执行以下操作:

您可以查询system.namespaces集合:

use test;
db.system.namespace.find( { name: 'test.' + collName } );
Run Code Online (Sandbox Code Playgroud)

像:

db.system.namespaces.find( { name: 'test.testCollection' } );
Run Code Online (Sandbox Code Playgroud)

哪个回报:

{ "name" : "test.testCollection", "options" : { "flags" : 1 } }
Run Code Online (Sandbox Code Playgroud)

或者当然,没什么.

另见:https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst

  • 在[mongo-native](https://github.com/mongodb/node-mongodb-native)中:`db.collection('system.namespaces').find().toArray(function(err,items){} )` (3认同)

Chr*_*isV 9

使用mongo-native驱动程序和Node.js 7.6+,我使用以下内容:

const collections = await db.collections();
if (!collections.map(c => c.s.name).includes(collName)) {
    await db.createCollection(collName);
}
Run Code Online (Sandbox Code Playgroud)


Rob*_*rto 7

从MongoDB 3.0开始,您只需运行:

db.getCollectionNames()
Run Code Online (Sandbox Code Playgroud)

它返回一个数组,其中包含当前数据库中所有集合的名称:

[ "employees", "products", "mylogs"]
Run Code Online (Sandbox Code Playgroud)

检查Mongo DB文档,或者如果您需要有关每个集合的更多信息,也可以使用db.getCollectionInfos()

  • 方法`getCollectionNames()`在node.js native lib中不可用. (4认同)
  • Dude,mongo的最新节点驱动程序是[`2.2`(链接)](https://mongodb.github.io/node-mongodb-native/).3.0甚至不存在. (3认同)

wee*_*ens 5

Node.js本机驱动程序中现在有一个listCollections方法.它返回当前数据库中所有集合的信息.您可以使用它来检查给定的集合是否存在:

collectionExists = function(name, cb) {
  mongoDb.listCollections().toArray(function(err, collections) {
    if (err) return cb(err);

    cb(null, collections.some(function(coll) {
      return coll.name == name;
    }));
  });
}
Run Code Online (Sandbox Code Playgroud)


ros*_*e_x 5

如果你使用mongodb 3.1.10。这是检查集合是否存在的方法。

MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
  if (err) throw err;

  var dbo = client.db("dbname");
  dbo.listCollections().toArray(function(err, items){
    if (err) throw err;

    console.log(items); 
    if (items.length == 0)
        console.log("No collections in database")  
  }); 
});
Run Code Online (Sandbox Code Playgroud)