将json中的数据导入到mongo容器docker

将json中的数据导入到mongo容器docker

本文介绍了将json中的数据导入到mongo容器docker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我开始学习docker。我知道如何创建容器,现在我尝试将data(json)导入到容器mongo中,但是我不知道该如何做。您可以帮我一个简单的解决方案吗?有了Docker-compose。

I have a question. I'm starting learn docker. I know how create a container, now I try import data(json) to my container mongo, but I don't know how I can make this. Can you help me and give a simple solution for this? With Docker-compose.

我的容器-mongo
我的文件-data.json

My container - mongoMy file - data.json

谢谢!

推荐答案

您不能仅使用docker-compose导入JSON。您需要将数据放在js文件中,并使用 /docker-entrypoint-initdb.d < js 文件挂载/ p>

You can not import JSON simply using docker-compose. you need to place the data in js file and mount the js file with /docker-entrypoint-initdb.d

示例js文件:

db = db.getSiblingDB("test");
db.article.drop();

db.article.save( {
    title : "this is my title" ,
    author : "bob" ,
    posted : new Date(1079895594000) ,
    pageViews : 5 ,
    tags : [ "fun" , "good" , "fun" ] ,
    comments : [
        { author :"joe" , text : "this is cool" } ,
        { author :"sam" , text : "this is bad" }
    ],
    other : { foo : 5 }
});

db.article.save( {
    title : "this is your title" ,
    author : "dave" ,
    posted : new Date(4121381470000) ,
    pageViews : 7 ,
    tags : [ "fun" , "nasty" ] ,
    comments : [
        { author :"barbara" , text : "this is interesting" } ,
        { author :"jenny" , text : "i like to play pinball", votes: 10 }
    ],
    other : { bar : 14 }
});

db.article.save( {
    title : "this is some other title" ,
    author : "jane" ,
    posted : new Date(978239834000) ,
    pageViews : 6 ,
    tags : [ "nasty" , "filthy" ] ,
    comments : [
        { author :"will" , text : "i don't like the color" } ,
        { author :"jenny" , text : "can i get that in green?" }
    ],
    other : { bar : 14 }
});

docker-compose

docker-compose

mongo:
    image: mongo
    container_name: mongo1
    environment:
      MONGO_INITDB_ROOT_USERNAME: test
      MONGO_INITDB_ROOT_PASSWORD: admin
      MONGO_INITDB_DATABASE: test
    volumes:
      - ./init.js:/docker-entrypoint-initdb.d/init.js
    ports:
      - 27017:27017

这篇关于将json中的数据导入到mongo容器docker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 09:48