Volumes configuration for mongodb in docker compose

I tried a long time to mount mongo db to my local storage, but the code comment( # – ./database:/data/db) can not successfully mount if I want to initialize db at the same time. Then I tried to create a volume as shown now(create a volumes section separately). and it works. Do you know the difference and why? could you kindly point it out? Thanks in advance.

before modification

version: '3.5'

services:

  mongodb:
    image: mongo
    container_name: mongodb
    restart: unless-stopped
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=root
    volumes:
      - ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      - ./database:/data/db    # not work 
  mongo-express:
    image: mongo-express
    container_name: mongo-express
    restart: unless-stopped
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=root
      - ME_CONFIG_MONGODB_ADMINPASSWORD=root
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
      - ME_CONFIG_BASICAUTH_USERNAME=xhp
      - ME_CONFIG_BASICAUTH_PASSWORD=xhp
    depends_on:
      - mongodb

after modification

version: '3.5'

services:

  mongodb:
    image: mongo
    container_name: mongodb
    restart: unless-stopped
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=root
    volumes:
      - ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      - mongodb:/data/db
      # - ./database:/data/db     not work 
  mongo-express:
    image: mongo-express
    container_name: mongo-express
    restart: unless-stopped
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=root
      - ME_CONFIG_MONGODB_ADMINPASSWORD=root
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
      - ME_CONFIG_BASICAUTH_USERNAME=xhp
      - ME_CONFIG_BASICAUTH_PASSWORD=xhp
    depends_on:
      - mongodb
volumes:
  mongodb:

Source: Docker Questions