Node.js - Mongoose - Check if a collection exists

前端 未结 3 1192
时光取名叫无心
时光取名叫无心 2020-12-01 18:20

I need to insert some data using mongoose but the name of the collection is provided by the user at the moment of the insertion, so I first have to check if the collection e

3条回答
  •  忘掉有多难
    2020-12-01 18:28

    Find collection in collection's list

    public function CollectionExists($collectionName)
        {
            $mongo = new Mongo();
            $collectionArr = $mongo->selectDB('yourrec')->listCollections();
            if (in_array($collectionName, $collectionArr)) {
                return true;
            }
            return false;
        }
    

提交回复
热议问题