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:
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
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
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