Guide on extracting data from a text file and inserting it into MongoDB using JavaScript

I've got a data file with hundreds of records that look like this:

{
"roadInfo": {
            "roadId": "1",
            "roadName": "Airport Boulevard",
            "laneCount": 5
        },
data:123
},
{
"roadInfo": {
            "roadId": "1",
            "roadName": "Airport Boulevard",
            "laneCount": 5
        },
data:123
} 
.
.
.
.
.
.
n

Currently, I'm manually inserting this JSON payload into a MongoDB collection. Is there a way to automate this process? Any suggestions or tips would be greatly appreciated.

Answer №1

To import data into your MongoDB database, you can utilize the mongoimport tool. Simply connect to your mongod instance using the mongo shell and execute a command like

mongoimport --db dbName --collection collectionName --file file.json
.

While primarily used for importing JSON files generated by mongoexport, mongoimport should work as long as it adheres to the strict data type requirements detailed here.

Another approach is creating a custom import script using Node.js with methods like fs.readFile() in conjunction with the mongodb driver or any suitable wrapper/ORM (such as mongoose, monk, mongoskin). You can refer to this helpful article on reading JSON files in Node.js.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to retrieve the properties of a JavaScript object

Currently I am working on a React webApp project and encountering difficulties when trying to access data within a JavaScript Object. Below is the code snippet in question: const user_position = System.prototype.getUserPosition(); console.log({user ...

Can the image quality be improved gradually while zooming in on Safari Mobile?

Are there any JavaScript or jQuery plugins available that can improve the quality of large images when zooming, similar to Google Earth or Maps? I'm looking for a solution that works well in mobile browsers, but a desktop version would also be accept ...

The impact of Ajax on search engine optimization is significant

My website relies heavily on ajax. I regularly update the hash values in the address bar to store browsing history, enabling the forward and back buttons to function properly. Here's an example scenario: site.com/directory#sports/1 site.com/director ...

Storing a javascript error object in mongoose: Best practices

Suppose we have an error object declared as follows: const error = new Error('Error'); I attempted to store this error object in a MongoDB field using the Object type, and even tried the Mixed type, but it ends up storing an empty Object. How c ...

Interactive loading of datalist choices using AJAX in the Firefox browser

I have recently decided to replace the Jquery UI autocomplete function on my website with HTML5 datalists that load dynamic options. After researching this topic extensively, I came across various answers on Stack Overflow, such as How do you refresh an HT ...

When using PHP for autocomplete, error messages are not displayed for non-existent rows returned from a MySQL query

My form currently has the ability to auto complete based on user input, querying the MySQL database to list all possible matches in a table and provide suggestions. However, I am facing difficulty in handling rows that do not exist and getting my PHP file ...

Creating a callback function within stored procedures using JavaScript Language Integrated Query in documentDB: A step-by-step guide

According to the documentation, the code snippets below are considered equivalent. However, I have observed that in the first case, I am able to perform operations on multiple documents within the callback function, whereas the map function in the latter s ...

Explore various Lists with unique headings by applying filters or conducting searches

I am looking for a script that can filter and search various lists with the same class. The script should display the headings of the lists if there is at least one matching search name. If there are no matching search names, the heading of the list should ...

The function batchWriteItem() has been known to produce unpredictable outcomes

Currently in my Node.js project, I am attempting to write records to a DynamoDB table using the batchWriteItem() method. When I call the insertTransactionDetails() function for the first time, I send 9 records to be inserted. On the second call to the sam ...

Obtain a picture and display it on a webpage using HTML

Currently, I am working on a project involving PHP and AJAX. While I have successfully managed to retrieve an image from a database, I am facing an issue where the image is displaying in its original size instead of being resized to fit within a 100px x 10 ...

Expanding the current module definition: A step-by-step guide

I have created a declaration file for an existing npm package, but it seems like one method was not declared. I attempted to add it, but encountered an error. Can someone please assist me? Here is the structure of the existing d.ts file: declare modul ...

Sending a json array from PHP

I spent several hours searching for solutions to my problem but couldn't find an answer. I'm trying to perform a search on the same page using jQuery, AJAX, and PHP. Unfortunately, the array from PHP is still returning undefined. Here is the P ...

`Nav arrow positioning problem in absolute position`

I'm experiencing an issue with the left navigation arrow in a portfolio viewer I'm using. It seems to be moving along with the window size and I can't seem to find a way to keep it contained within my div and stationary. The plugin responsib ...

What is the best way to extract data from a series of nested JSON objects and insert it into a text field for editing?

I am facing a challenge with appending a group of nested JSON objects to a text field without hard coding multiple fields. Although I have used the .map functionality before, I am struggling to make it work in this specific scenario. const [questions, setQ ...

Having trouble with React state not updating accurately. The increment and decrement function is not functioning properly on the initial click, but seems to work fine after

React state updates not functioning properly. The increase and decrease functions are not working on the first click but work after subsequent clicks. The subtotal value is consistently incorrect, showing as 94.50 instead of the expected 105 for a quanti ...

Tips for resizing tab components in Materials-UI

I am currently developing a basic app that showcases heart rate, blood glucose levels, and other measurements. I am utilizing React and Redux for the development process and incorporating Materials-UI for the user interface. For displaying these metrics, ...

Is there a way to ensure that a DIV displays inline and aligned at the same distance from the right in my HTML layout?

Within my HTML template, I have a collection of categories and subcategories. Each category can contain anywhere from 0 to n subcategories. I am currently displaying these using nested for loops: Category 1 Subcategory 1 Subcategory 2 Category 2 Su ...

Experiencing difficulties while attempting to organize an array?

// const first = data.groups_with_selected[7]; // const second = data.groups_with_selected[20]; // data.groups_with_selected.splice(2, 0, first, second); // data.groups_with_selected.splice(9, 1) // data.groups_with_selected ...

Adding a new object to a schema on the fly - is it possible?

In my collection, the schema looks like this- var dataSchema = new mongoose.Schema({ name:{type:String}, info:{type:mongoose.Schema.Types.Mixed} }); When I save a document in the collection, it appears as- { name:johnDoe,info:{}} Now, I would like ...

What is the best way to automatically update a SUM query in the database whenever a new item is added or an existing item is modified?

I am in the process of creating a point of sale system and have a query that calculates the sum from my database. How can I ensure that the total value on the page updates automatically whenever a new item is added or an existing item is updated? <?php ...