Login

MongoDB data load

Author:
jose_lpa
Posted:
January 3, 2013
Language:
Python
Version:
1.4
Score:
0 (after 0 ratings)

This snippet loads data from JSON files into a MongoDB database.

The code is related with the other snippet MongoDB data dump.

To get it working, just create a MONGODB_NAME variable in settings, holding the name of your Mongo database. This can be edited to fit more your needs. The snippet requires Pymongo, since it uses its bson module and the MongoClient.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings

from optparse import make_option
from bson.json_util import loads
from pymongo import MongoClient
from os.path import splitext


class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--collection', '-c',
                    dest='collection',
                    help='MongoDB collection where the data will be loaded.'),
    )

    help = "Loads data from files into the MongoDB database.\n\n" \
           "Usage: manage.py mongo_load FILE"

    def handle(self, *args, **options):
        if not args:
            raise CommandError('No data filename provided.')

        collection = options.get('collection')

        connection = MongoClient()
        db = connection[settings.MONGODB_NAME]

        if not collection:
            name = splitext(args[0])[0]
            items = db[name]
        else:
            items = db[collection]

        with open(args[0], 'r') as f:
            for line in f:
                items.insert(loads(line))

More like this

  1. Generate and render HTML Table by LLyaudet 5 days, 9 hours ago
  2. My firs Snippets by GutemaG 1 week, 1 day ago
  3. FileField having auto upload_to path by junaidmgithub 1 month, 2 weeks ago
  4. LazyPrimaryKeyRelatedField by LLyaudet 1 month, 3 weeks ago
  5. CacheInDictManager by LLyaudet 1 month, 3 weeks ago

Comments

Please login first before commenting.