question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MongoDB: database name must be a string

See original GitHub issue

Issue type:

[ * ] question [ * ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ * ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[ * ] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Create a default project with command

typeorm init --name test --database mongdb --express
cd test
yarn install
yarn start

You can see the following error :

{ MongoError: database name must be a string
     at Function.MongoError.create (/app/node_modules/mongodb-core/lib/error.js:31:11)
     at validateDatabaseName (/app/node_modules/mongodb/lib/db.js:1680:57)
     at new Db (/app/node_modules/mongodb/lib/db.js:156:3)
     at Db.db (/app/node_modules/mongodb/lib/db.js:1208:12)
     at MongoQueryRunner.getCollection (/app/src/driver/mongodb/MongoQueryRunner.ts:803:40)
     at MongoQueryRunner.<anonymous> (/app/src/driver/mongodb/MongoQueryRunner.ts:272:27)
     at step (/app/node_modules/typeorm/driver/mongodb/MongoQueryRunner.js:32:23)
     at Object.next (/app/node_modules/typeorm/driver/mongodb/MongoQueryRunner.js:13:53)
     at /app/node_modules/typeorm/driver/mongodb/MongoQueryRunner.js:7:71
     at new Promise (<anonymous>)
       name: 'MongoError',
       message: 'database name must be a string',
       driver: true }

My ormconfig.json file :

{
   "type": "mongodb",
   "database": "test",
   "synchronize": true,
   "logging": false,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

60reactions
Fusseldiebcommented, Jan 21, 2019

I faced this issue and the problem was that somehow it previously worked without specifying a collection name, but now it needs one.

So…

mongoose.connect(
  "mongodb://localhost",
  {
    useNewUrlParser: true
  }
);

gave this error. But then I specified the collection name admin in this way:

mongoose.connect(
  "mongodb://localhost/admin",
  {
    useNewUrlParser: true
  }
);

and the error vanished.

Thanks documentation

13reactions
glorious73commented, Dec 19, 2018

THIS ANSWER WAS EDITED AS THE PREVIOUS ONE LEAD TO OTHER ERRORS I was able to fix the error by adding the following to mongoose.connect() function:

  1. After connecting, pass the following parameter: useMongoClient: true
  2. Here is how it would look like: image
  3. Run again, and it should work. screenshot from 2018-12-19 21-17-13
Read more comments on GitHub >

github_iconTop Results From Across the Web

'database name must be a string' Error when connecting with ...
I am attempting to connect to MongoDB via the package mongoose , but get an error along the lines of MongoError: database name...
Read more >
MongoError: database name must be a string - JavaScript
I'm using replit and I can't connect to my database for the first challenge, Here's what my code looks like: const mongoose ...
Read more >
Connection String URI Format — MongoDB Manual
Set the Kerberos service name when connecting to Kerberized MongoDB instances. This value must match the service name set on MongoDB instances to...
Read more >
Fix “index name must be a string” when Dropping Multiple ...
If you encounter the “index name must be a string” error when dropping multiple indexes in MongoDB, it's probably because you're passing the ......
Read more >
MongoDbUtils (Spring Data MongoDB 1.9.5.RELEASE API)
Mongo mongo, String databaseName). Obtains a DB connection for the given Mongo instance and database name. Parameters: mongo - the Mongo instance, must...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found