Fineone stuck on test database named

I have to connect like this on database name test1 :

mongodb://user:pass@www.server.com:27017?authMechanism=SCRAM-SHA-1&authSource=test1

but when I used findone() :

db.findOne(condition, function(err, data2)

I got an error :

MongoServerError: not authorized on test to execute command { find: “userTemp_1.0”, filter: { userID: /user02/i }, projection: {}, limit: 1, singleBatch: true, batchSize: 1, lsid: { id: UUID(“d7d142ad-1830-43a8-9d9c-6559aec5cdcc”) }, $db: “test” }

why database change to test from test1 on my connection

thank you in advance

It did not. You specify test1 as the authSource not as the default database. The default database is test when it is not specify it. You specify the default database after the port number and before the options which start after the question mark. Try:

mongodb://user:pass@www.server.com:27017/test1?authMechanism=SCRAM-SHA-1&authSource=test1

See https://docs.mongodb.com/manual/reference/connection-string/ for more details.

1 Like

its not work, on connection mongoose already connected to database “test1”, but when I used findone this function still work on “test” named database

this problem only appear on ubuntu side, I already try on window still dont have a problem, I have to run on ubuntu api server

As steevej says you can specify your database just after the host and port in the connection string, you example connection string is missing that but good coding/usage practise is to explicitly specify the database in the command.

Set db to the database you actually want using the correct call for your API - in the shell it’s

db = db.getSiblingDB("test1")

in node

db = client.db("test1")
1 Like

then I used

db.findOne(condition, function(err, data2) {}

they error throwing “cant auth on test” but “I connected on test1 already”


db = client.db(“test1”)
this is not work my db from schema

const db = require(’./userTemp.model’);

this is schema

Where is db being defined?

Have you tried specifying the database you want in the URI like @steevej suggested.

You Authenticate against one database (Which should always be admin not ever test1)

You then select a database in the URI or in your code to query against.

//Set up default mongoose connection
var mongoDB = 'mongodb://127.0.0.1/my_database';
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true});

mongoose.connction.useDB("anydbyouwant")

for sure the problem is not mongoose mongoose already connected to test1

but when I used findone() this function still point on test database not test1

this case happen on ubuntu only, I have try on window and ubuntu

on window10 my code already working but ubuntu still stuck on test database

How do you know that?

When using non-mongoose application like the mongo shell and using your URI with /test1 after the port number, I see that I am using the test1 database:

With /test1 after the port number and before the question mark

: steevej @ mint-1 ; mongo 'mongodb://UserName:Password@HostName:27017/test1?authMechanism=SCRAM-SHA-1&authSource=test1'
MongoDB shell version v4.0.25
connecting to: mongodb://HostName:27017/test1?authMechanism=SCRAM-SHA-1&authSource=test1&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("22315b5e-ebad-44b0-8714-133b08488c63") }
MongoDB server version: 5.0.3
WARNING: shell and server versions do not match
> db
test1
> exit
bye

and now without /test1 after the port number and before the question mark.

: steevej @ mint-1 ; mongo 'mongodb://UserName:Password@HostName:27017/?authMechanism=SCRAM-SHA-1&authSource=test1'
MongoDB shell version v4.0.25
connecting to: mongodb://HostName:27017/test?authMechanism=SCRAM-SHA-1&authSource=test1&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("22315b5e-ebad-44b0-8714-133b08488c63") }
MongoDB server version: 5.0.3
WARNING: shell and server versions do not match
> db
test
> exit
bye

So it may not be moogoose, but it is certainly not the underlining mongo driver. And with such a big difference of behavior between Windows and Ubuntu, it must be something else that you do in your setup.

By the way change the password of the user Tyme. Although you edited your previous post the real URI with the real password of this user is still readable.

This is while connected to your server:

> use test1
switched to db test1
> show collections
> use test
switched to db test
> show collections
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> use test1
switched to db test1
> db.getUsers()
[
	{
		"_id" : "test1.Tyme",
		"userId" : UUID("86ea443d-e7a0-4a51-b2a1-265da2caa972"),
		"user" : "Tyme",
		"db" : "test1",
		"roles" : [
			{
				"role" : "dbOwner",
				"db" : "test1"
			}
		],
		"mechanisms" : [
			"SCRAM-SHA-1",
			"SCRAM-SHA-256"
		]
	}
]
> exit
bye

1 Like

because when I change user , password or authSource to non correct
mongoose will throw an error “not authorized cant connect to database”
and I used database name after the port number already as your suggest
but my case I can connected by mongoose already
but when findone() db will change back to test

I tried this on window so work on test1 but ubuntu work on test that difference here
my main server on ubuntu

and I used your syntax like this already
‘mongodb://UserName:Password@HostName:27017/test1?authMechanism=SCRAM-SHA-1&authSource=test1’

on your example you used test1 behind port number on 2 cases but first case db is test1 and 2nd case db is test
that all I see not difference command but result not the same

That is a cut-n-paste error from my part. The one that print test did no have /test1 as DB after port. I pasted the same command twice.

Do not do that. The user, the password and the authSource of the URI were all correct.

But I repeat

because I can still connect to your server.

ty in advance
I will try if have any update I will come here again