14

I am attempting to seed my MongoDB in Docker. my docker-compose.yml file is pretty straight forward, here is my volume though:

...
volumes:
      - ./mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
      - ./mongo/mongod.conf:/etc/mongod.conf:ro
      - ./mongo/mongo-volume:/data/db
...

I have init-mongo.js mapped as a volume, here is its contents:

db.createUser({
  user: 'root',
  pwd: 'toor',
  roles: [
    {
      role: 'readWrite',
      db: 'testDB',
    },
  ],
});
db.createCollection('users', { capped: false });
db.createCollection('test', { capped: false });

db.test.insert([
  { "item": 1 },
  { "item": 2 },
  { "item": 3 },
  { "item": 4 },
  { "item": 5 }
]);

The user root and the database testDB as well as the collections users and test are all created with no problem.

The issue is that db.test.insert([]) seems to be missed all together, and NO documents are inserted into the test collection.

Any Ideas why that is? I would absolutely love to get this working and would really appreciate any assistance with this.

Thanks, -Kevin

1
  • the create user runs with no problem, I get an error on the use line: uncaught exception: SyntaxError: unexpected token: identifier : @/docker-entrypoint-initdb.d/mongo-init.js:14:4 failed to load: /docker-entrypoint-initdb.d/mongo-init.js I have changed the file a bit, but the error is on the "use testdb" line (no quotes in my file) Commented Oct 24, 2020 at 14:08

2 Answers 2

19

Change your init-mongo.js file to this to select the new database first, then create collections and finally seed data into them:

db.createUser({
    user: 'root',
    pwd: 'toor',
    roles: [
        {
            role: 'readWrite',
            db: 'testDB',
        },
    ],
});

db = new Mongo().getDB("testDB");

db.createCollection('users', { capped: false });
db.createCollection('test', { capped: false });

db.test.insert([
    { "item": 1 },
    { "item": 2 },
    { "item": 3 },
    { "item": 4 },
    { "item": 5 }
]);

Then login to mongo shell (use mongo -u [admin_username] -p) and check the data in the testDB collection:

use testDB;
db.getCollection('test').find()

That will show:

{ "_id" : ObjectId("5f947906cc845282e5711728"), "item" : 1 }
{ "_id" : ObjectId("5f947906cc845282e5711729"), "item" : 2 }
{ "_id" : ObjectId("5f947906cc845282e571172a"), "item" : 3 }
{ "_id" : ObjectId("5f947906cc845282e571172b"), "item" : 4 }
{ "_id" : ObjectId("5f947906cc845282e571172c"), "item" : 5 }
Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to import 'Mongo' in this js file to use 'Mongo()'?
For those confused about the new Mongo().getDB() piece, there's also db.getSiblingDB() which has the same effect since it's in the same connection. No imports or anything required since it's all part of the mongosh (depending on if you're mongodb >= 6.0) or mongo shell.
0

I think you're connected to the admin db right? Users are generally created on the admin db.

Try reinitialising your db with "testDB" db before you run the insert.

Comments

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.