首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将json中的数据导入mongo集装箱码头

问将json中的数据导入mongo集装箱码头
EN

Stack Overflow用户
提问于 2019-10-02 13:05:50
回答 1查看 614关注 0票数 0

我有个问题。我开始学码头了。我知道如何创建一个容器,现在我尝试将数据(Json)导入到我的容器mongo,但我不知道如何实现。你能帮我并给出一个简单的解决方案吗?与码头-写作。

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

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-02 13:36:13

您不能简单地使用导入JSON。您需要将数据放在js文件中并使用/docker-entrypoint-initdb.d挂载该/docker-entrypoint-initdb.d文件。

当容器第一次启动时,它将执行在.sh中找到的扩展名为/docker-entrypoint-initdb.d和.js的文件。文件将按字母顺序执行。如果存在.js文件,将使用MONGO_INITDB_DATABASE变量指定的数据库由mongo执行,或者以其他方式进行测试。您还可以在.js脚本中切换数据库。

启动一个新实例

示例js文件:

代码语言:javascript
复制
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 }
});

码头工

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58202154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档