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 8815287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:20:18+00:00 2026-06-14T04:20:18+00:00

I need to do the following in my code: Read data from a text

  • 0

I need to do the following in my code:

  • Read data from a text file
  • Convert the data into Json
  • Upsert the data into MongoDB

Here is an example of how the content of text file is going to look:

{
   "S": "someString" <- Type String when inerted in mongodb
   "N": 123          <- Type Int32
   "F": 12.3         <- Type Double
   "D": ?            <- Need to be Type DateTime when inerted in mongodb
}

I don’t know what I am suppose to have in the place of the “?” so when I use bson.json_util.loads function in python it can properly convert the text file into Json which later can be inserted into mongoDB.

Here is the code that does the load and insert:

with open('data.txt') as f:
    data = json_util.loads(f.read())
    db[dbName][colName].update({'_id': id}, data, upsert=True,safe=True)

I would appreciate it if someone can give an example of how the file should be formatted. (If your example can include even more complex Bson types like type “binary” or “code” that would be nice too 🙂 )

  • 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-14T04:20:20+00:00Added an answer on June 14, 2026 at 4:20 am

    Mongo’s representation of datetimes is {"$date": number-of-milliseconds-since-epoch}. In your example:

    {
       "S": "someString",
       "N": 123,
       "F": 12.3,
       "D": {"$date": 1352540684243}
    }
    

    D will generate a datetime field when written to mongo.

    See the documentation for mongo json extensions.

    You can also easily extend json_util to program your own extensions, for example, for ISO-formatted datetimes:

    import json, dateutil.parser, bson.json_util
    
    a = """{
        "mydate": {"$isodate": "2012-11-01T20:19:55.782Z"}
    }"""
    
    def my_hook(dct):
        if '$isodate' in dct:
            return dateutil.parser.parse(dct['$isodate'])
        return bson.json_util.object_hook(dct)
    
    obj = json.loads(a, object_hook=my_hook)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following basic code to read a text file from a StreamReader:
I used the following piece of code to read data from files as part
I have the following code that uses Sequence objects to read data from a
In the following code I need to print output from the variables $stout ,
i have the following code ..i need to loop through end of the file
I am using C# and OleDb to read data from an excel 2007 file.
At the moment, I am generating xml and json data using the following code:
I'm using the following code to read an xml from my server and its
I am fiddling around with the following code. However, I need the file name
The following code generates a MS Word document in portrait format. But I need

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.