我需要判断某个数据库上是否存在一个集合,如果不存在,就创建它.我知道这一点
db.createCollection(collName, {strict:true}, function(error, collection))
在创建集合collName
之前判断集合collName
是否存在,并设置error
对象.但我需要一个独立的函数来判断.
我需要判断某个数据库上是否存在一个集合,如果不存在,就创建它.我知道这一点
db.createCollection(collName, {strict:true}, function(error, collection))
在创建集合collName
之前判断集合collName
是否存在,并设置error
对象.但我需要一个独立的函数来判断.
In MongoDB 3.0 and later, you have to run a command to list all collections in a database:
use test;
db.runCommand( { listCollections: 1 } );
尽管在使用默认存储引擎(MMAPv1)时,查询system.namespaces
仍能工作,但不能保证它适用于其他引擎,如WiredTiger.
Before MongoDB 3.0 you need to do the following:
您可以查询system.namespaces
个集合:
use test;
db.system.namespace.find( { name: 'test.' + collName } );
比如:
db.system.namespaces.find( { name: 'test.testCollection' } );
返回:
{ "name" : "test.testCollection", "options" : { "flags" : 1 } }
当然,也没什么.
See also: 100