简体   繁体   English

MongoDB导入字段设置的不同集合

[英]MongoDB import to different collections set by a field

I have a file called data.json and extracted with mongoexport , with the following structure: 我有一个名为data.json的文件,并使用mongoexport提取,其结构如下:

{"id":"63","name":"rcontent","table":"modules"}
{"id":"81","name":"choicegroup","table":"modules"}
{"id":"681","course":"1242","name":"Requeriments del curs","timemodified":"1388667164","table":"page"}
{"id":"682","course":"1242","name":"Guia d'estudi","timemodified":"1374183513","table":"page"}

What I need is to import this file into my local mongodb with a command like mongoimport or with pymongo , but storing every line in the collection named after the table value. 我需要的是使用诸如mongoimportpymongo类的命令将此文件导入到本地mongodb ,但将每行存储在以表值命名的集合中。

For example, the collection modules would contain the documents 例如,收集模块将包含文档

{"id":"63","name":"rcontent"} and {"id":"81","name":"choicegroup"}

I've tried with mongoimport but I haven't seen any option which allows that. 我已经尝试过mongoimport但是我还没有看到任何允许的选择。 Does anyone know if there is a command or a method to do that? 有谁知道是否有命令或方法来做到这一点?

Thank you 谢谢

The basic steps for this using python are: 使用python的基本步骤是:

  1. parse the data.json file to create python objects 解析data.json文件以创建python对象

  2. extract the table key value pair from each document object 从每个文档对象中提取table键值对

  3. insert the remaining doc into a pymongo collection 将其余文档插入pymongo集合

Thankfully, pymongo makes this pretty straightforward, as below: 值得庆幸的是,pymongo使这一过程变得非常简单,如下所示:

import json

from pymongo import MongoClient

client = MongoClient()  # this will use default port and host
db = client['test-db']  # select the db to use
with open("data.json", "r") as json_f:
    for str_doc in json_f.readlines():
        doc = json.loads(str_doc)
        table = doc.pop("table")  # remove the 'table' key 
        db[table].insert(doc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM