Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8290377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:47:45+00:00 2026-06-08T12:47:45+00:00

I’m currently attempting to load several text files into MongoDB (they’re in JSON format).

  • 0

I’m currently attempting to load several text files into MongoDB (they’re in JSON format).

I tried using an OS walk, but I seem to be having trouble.
My current method is:

>>> import pymongo
>>> import os
>>> import json
>>> from pymongo import Connection
>>> connection = Connection()
>>> db = connection.Austin
>>> collection = db.tweets
>>> collection = db.tweet_collection
>>> db.tweet_collection
Collection(Database(Connection('localhost', 27017), u'Austin'), u'tweet_collection')
>>> collection
Collection(Database(Connection('localhost', 27017), u'Austin'), u'tweet_collection')
>>> tweets = db.tweets
>>> tweet = open(os.path.expanduser('~/Tweets/10_7_2012_12:09-Tweets.txt'),'r')
>>> for line in tweet:
...      d = json.loads(line)
...      tweets.insert(d)
... 

For inserting a single Tweet.
I want to be able to open multiple files and run that same piece of code, namely the for loop that turns the JSON into python dictionaries and inserts it into the collection, autonomously.

Does anyone have a solid example of how to do this, complete with an explanation?

While we’re on the topic, I’m attempting to use MongoDB with a poor understanding of databases (silly and stupid, I know), but MongoDB can support multiple instances of databases at the same time, and stores collections, which are groups of documents, and you can insert individual documents, correct?

(Also, please ignore the inconsistency between the collections tweets and tweet_collection.. I was just experimenting to get a better understanding)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:47:47+00:00Added an answer on June 8, 2026 at 12:47 pm

    untested

    from glob import iglob
    import os.path
    import pymongo
    import json
    
    for fname in iglob(os.path.expanduser('~/Tweets/*.txt')):
        with open(fname) as fin:
            tweets = json.load(fin)
            for tweet in tweets:
                db.tweets.insert(tweet)
    

    This loops over all the filenames in ‘~/Tweets/*.txt’, opens it, and loads 1 or more tweets from the file into a Python dictionary – note the use of .load instead of .loads – the difference being .load() takes a file-like object while .loads() takes a string. Then for each tweet, inserts that into the database. (Note I’ve used db.tweets.insert instead of tweets = db.tweets as I personally find the ‘db’ prefix a reminder it’s a DB op and not some other object)

    As to your understanding on MongoDB re: DB’s/collections/documents – yes, you’re correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a bunch of posts stored in text files formatted in yaml/textile (from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.