Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing JSON in MongoDB

Tags:

mongodb

I m trying to store some data in MongoDB, I am not sure of the type of data that I am being provided with as I am getting it from a formbuilder(https://github.com/kevinchappell/formBuilder) that I am using.

I am getting the data from:

  document.getElementById('getJSON').addEventListener('click', function() {
  var ans = formBuilder.actions.getData('json', true);

  //console.log(ans);
  //var ans2 = JSON.parse(ans);

  alert(ans);

  console.log(ans);

  $.ajax({
        type: "POST",
        data: ans,
        url: "/j",
        success: function(){
            console.log('success');
        }
    });

    document.forms["myForm"].submit();

});

It reaches my back end as so:

//FETCHING THE JSON OF THE CLAUSE FORM CREATED BY THE ADMIN
router.post('/j', function(req, res, next) {

req.session.fdata = req.body; //JSON FETCHED FROM JAVASCRIPT USING AJAX, STORED IN SESSION


if(req.session.fdata==null) //CHECKING IF WE ARE RECEIVING VALUES
{
    res.redirect('/admin');
}
else {

    mongo.connect(url, function (err, db) {

        assert.equal(null, err);

        //var jfdata = JSON.parse(req.session.fdata);

        db.collection('clauses').insertOne(req.session.fdata, function (err, result) {

            console.log('New Clause Added');
            console.log(req.session.fdata);
            db.close();

        });
    });

    res.redirect('/admin');
}
});

I insert it into the DB and it looks fine in the DB but on retrieval I cant seem to access the inner portions of the data. Is my data in the wrong format? Is it JSON or a JS object?

it looks like so in the DB:(the db is empty before insertion)enter image description here

This is what the console prints

 [ { _id: 596de520ef77eb2614cd1e47,
 '[\n\t{\n\t\t"type": "number",\n\t\t"label": "Number",\n\t\t"description": 
 "total number",\n\t\t"placeholder": "0",\n\t\t"className": "form-
 control",\n\t\t"name": "number-1500374279764"\n\t}\n]': '' },
 { _id: 596de520ef77eb2614cd1e48 } ]
like image 942
Abhishek Malik Avatar asked Oct 31 '25 13:10

Abhishek Malik


1 Answers

The data you are trying to save does not seem right to me.

What you are getting is a string of the JSON object. You have to use JSON.parse to convert it to a proper JSON object.

JSON.parse('[\n\t{\n\t\t"type": "number",\n\t\t"label":"Number",\n\t\t"description": "total number",\n\t\t"placeholder": "0",\n\t\t"className": "form-control",\n\t\t"name": "number-1500374279764"\n\t}\n]')    

After that, you can form the data and insert in DB.

var query = {
             array : [{"type": "number",
                    "label": "Number",
                    "description": "total number",
                    "placeholder": "0",
                    "className": "form-control",
                    "name": "number - 1500374279764"}]
        }

db.collection('clauses').insertOne(query, function (err, result) 
{
        db.close();
});

Let me know if it helps!

like image 133
Shrabanee Avatar answered Nov 02 '25 16:11

Shrabanee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!