12

I setup MongoDB docker container following this blog: https://medium.com/@kristaps.strals/docker-mongodb-net-core-a-good-time-e21f1acb4b7b

and when I run .net core app, save one todo, I manage to query it back, and it seems that everything works (todo collection is created, db is created, todo is saved). But when I access mongo db container using 'docker exec -it mongodb bash' and try to get dbs with 'show dbs' it retrieves empty list. Also, this docker compose boots up mongo express container which also doesn't list new db and collection. Do you know what can be an issue?

Cheers

8
  • 1
    How are you connecting to the mongodb ? Is authentication enabled for the mongod instance? If yes, is the authentication success full? Try running db.adminCommand( { listDatabases: 1 } ) Commented Jun 10, 2019 at 22:34
  • 1
    As per the blog, authentication is enabled, so you should use the user name and password to connect to mongodb. The writes from .net core app may not be successful, that may be the reason you are not seeing any result in the mongodb. Commented Jun 10, 2019 at 22:41
  • 1
    @Mani i tried now to remove containers and to start them up again without auth and it is the same, writes are successful still because endpoint for returning all items from collection returns them correctly Commented Jun 10, 2019 at 22:46
  • 1
    It looks there is something wrong with api code, I stopped all docker containers and it still manages to save and retrieve items... I don't know whats the magic here, they are not stored in memory because I tried to restart api as well Commented Jun 10, 2019 at 22:56
  • 2
    Are you running any other instance of mongodb in your local machine? Commented Jun 10, 2019 at 23:13

1 Answer 1

40

It's likely that you have authentication enabled (username and password). This means that you have to first authenticate in the shell before you can list the databases.

Here's how that can be done (extra steps added for googlers):

    $ docker ps // list images

    IMAGE
    my-mongodb-container

$ docker exec -it my-mongodb-container bash

// -it is a flag for 'interactive terminal', bash is the shell we'll use

root@example:/# mongo 
// enter mongodb shell

> use admin 
// switched to db admin

> db.auth("username", "password");
// 1

> show dbs
admin  0.000GB
config 0.000GB
local  0.000GB
mydb   0.000GB

> use mydb
// switched to db mydb

You are now able to run mongodb shell commands inside your mongodb instance - so things like 'show collections' will work.

Hope that helps someone! This stuff is sometimes very opaque :)

Sign up to request clarification or add additional context in comments.

3 Comments

Great answer! Can you extend the example to list records from a table (a. la. "select * from <table>"), please?
If we do not have mongo installed. Is there any other way we can access to that data.
db.collectionName.find() command will list all entries to the collection as JSON. Change collectionName with your collection's name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.