1

I currently have a JSON file titled "data.json". In this file, I only have an array of people's names, like this:

"data":
[
    {
        {
        "name":"John",
        "age":30,
        }

        {
        "name":"Mark",
        "age":45,
        }   
    }
]

My goal is to write a script to insert this data into mongoDB. Could I write this directly in my JSON file? How would I go about this? Thanks!

5
  • You need to structure the JSON from the javascript and sent it off to something (service, PHP/C#/Java/Node.js etc.) which can actually insert it into the database. You cannot write something within the JSON file itself to do this. Commented Dec 5, 2018 at 22:01
  • Alright. So based on what you have said, in which language/file would I go about creating that script? I know you said I would have to "send" it to something, such as javascript (I'm assuming)? How would I write the script itself? Commented Dec 5, 2018 at 22:02
  • You would structure the json in the javascript file and then send it off to a service. That service would be the one writing the structured json received from the javascript into the database. The JSON file does nothing but hold data. It's only a way to structure data. There are many different types of services you can write in many languages. That choice is up to you. Commented Dec 5, 2018 at 22:08
  • I'm curious what your app/project is supposed to do. MongoDB and the JSON file are data storage destinations. You shouldn't need to keep track of data in both sources. Commented Dec 5, 2018 at 22:16
  • Andy, I am currently trying to create a data file, and then a script to insert that data into MongoDB. In more detail, my script should insert an array of JSON objects. To do this, I initially created a JSON file and made an array there (as I posted above). Now, I am trying to somehow write a script to read that data into MongoDB. Is this the incorrect method? Commented Dec 5, 2018 at 22:22

2 Answers 2

2

You can make this script with NodeJS

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/"; // Your mongodb url here

enter code here`MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var myobj = { name: "Company Inc", address: "Highway 37" };
  dbo.collection("customers").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log("The document got inserted.");
    db.close();
  });
});

You can also insert it manually through the GUI program or directly through your MongoDB command line

db.collection.insert('"data":[{"name":"John","age": 30},{"name": "Mark", "age": 45}]')
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting. I was reading this webpage: docs.mongodb.com/manual/tutorial/… I was wondering if I could create a new Javscript file, and write my script in there? Then, I could execute my JS file directly from Mongo?
I am not using MongoDB that often so I never encountered with those scripts. I usually do things manually or with node. You can check this thing out on Youtube. Hope you will find the best way to do that!
-1

create a post route using nodejs to add data to your collection in mongodb

1 Comment

A post route... so by that, I'm guessing you mean create a new JS file, and I can create my script there? Hmm. I'm still a bit confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.