0

I am new to both Docker and Mongo DB. I have kubernetes cluster that has separate MongoDB instances for each microservice (auth, firm, company, permissions). I have an issue that I want to see what's inside the auth MongoDB collection so I can appropriately delete the culprit. So my question is how can I mongosh into that database?

auth-depl.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth
  template:
    metadata:
      labels:
        app: auth
        # tier: backend
    spec:
      containers:
        - name: auth
          image: us.gcr.io/mavata/auth
          # image: bridgetmelvin/auth
          env:
            - name: MONGO_URI
              value: 'mongodb://auth-mongo-srv:27017/auth'
            - name: JWT_KEY
              valueFrom:
                secretKeyRef:
                  name: jwt-secret
                  key: JWT_KEY
---
apiVersion: v1
kind: Service
metadata:
  name: auth-srv
spec:
  selector:
    app: auth
  ports:
    - name: auth
      protocol: TCP
      port: 4000
      targetPort: 4000

auth-mongo-depl.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-mongo-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth-mongo
  template:
    metadata:
      labels:
        app: auth-mongo
        # tier: database
    spec:
      containers:
        - name: auth-mongo
          image: mongo
---
apiVersion: v1
kind: Service
metadata:
  name: auth-mongo-srv
spec:
  selector:
    app: auth-mongo
  ports:
    - name: db
      protocol: TCP
      port: 27017
      targetPort: 27017

registered-user.ts:

import mongoose from 'mongoose';
import { Firm } from './firm';
import { BadRequestError } from '@mavata/common';

// 1. Interface #1 WHAT IT TAKES TO CREATE A REGUSER
// An interface that describes the properties
// that are requried to create a new RegUser
interface RegUserAttrs {
  email: string;
  firm: string;
  firmId?: string;
  root?: boolean;
}

// Interface #2 WHAT THE ENTIRE COLLECTION OF RegUSERS LOOKS LIKE (METHODS ASSOCIATED WITH THE RegUSER MODEL)
// An interface that describes the properties
// that a RegUser Model has
interface RegUserModel extends mongoose.Model<RegUserDoc> {
  build(attrs: RegUserAttrs): RegUserDoc;
}

// Interface #3 WHAT PROPERTIES A SINGLE RegUSER HAS
// An interface that describes the properties
// that a RegUser Document has
interface RegUserDoc extends mongoose.Document {
  email: string;
  firm: string;
  firmId: string;
  root: boolean;
}

const regUserSchema = new mongoose.Schema({
  email: {
    type: String,
    required: true
  },
  firm: {
    type: String,
    required: true
  },
  firmId: {
    type: mongoose.Schema.Types.ObjectId,
    required: true
  },
  root: {
    type: Boolean,
    default: false,
    required: true,
  }
},
{
  // UNCOMMON to be transformation logic inside of a model
  // this is defining how some data inside of our model should be VIEWED and transmitted along the network
  toJSON: {
    transform(doc, ret) {
      ret.id = ret._id;     // rename _id property to id
      delete ret._id;       // delete the _id property off the object
      delete ret.firm;
      delete ret.__v;       // delete useless property
    }
  }
});


// middleware function
// if we used arrow function rather than function declaration as seen below, then
// references to 'this' would refer to THIS registered-users.ts file rather than 
// refer to the 'RegUser' trying to be persisted in the database
regUserSchema.pre('save', async function(done) {
  const domain = '@' + this.email.split('@')[1];
  const exists = await Firm.findOne({ domain });
  if (exists) {
    this.set('firmId', exists._id);
    done();
  } else {
    console.log('welp')
    throw new BadRequestError('That email domain is not registered with a firm');
  }
});

regUserSchema.statics.build = (attrs: RegUserAttrs) => {
  return new RegUser(attrs);
};

const RegUser = mongoose.model<RegUserDoc, RegUserModel>('RegUser', regUserSchema);

export { RegUser };

Dockerfile:

FROM node:alpine

WORKDIR /app
COPY package.json .
RUN npm install
COPY . .

CMD ["npm", "start"]
melv3223
  • 67
  • 5
  • Does this answer your question? [How to start a mongodb shell in docker container?](https://stackoverflow.com/questions/32944729/how-to-start-a-mongodb-shell-in-docker-container) – Mikolaj Oct 25 '22 at 16:46
  • not necessarily... when I run `docker run -d -p 29000:27017 --name mongodb-6 mongo:6` then `docker exec -it mongodb-6 mongosh` then `show collections` no collections show up. I want to run mongosh within my auth-mongo container.. how would I do that? – melv3223 Oct 25 '22 at 17:43
  • Did you select db (`use `) before show collections? `show dbs` command could be useful. – Mikolaj Oct 25 '22 at 18:23

0 Answers0