0

I'm trying to format a huge js file so I can insert it into a MongoDB collection and read it via AJAX but I'm having trouble with the syntax. It goes something like this:

var MyVariable1 = [ { 
  ThingsToGet: [ 
  { 
     first_thing: "SOMETHING ONE",
     second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, "x"], [6, "x"]]
     third_thing: 0,
     fourth_thing: []
     },{ 
     first_thing: "SOMETHING TWO",
     second_thing: [[1, 0], [2, 5], [3, 4], [4, 4], [5, "x"], [6, "x"]],
     third_thing: 0,
     fourth_thing: []
     },{ 
     first_thing: "SOMETHING THREE",
     second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, 2], [6, 2]],
     third_thing: 0,
     fourth_thing: []
     },
   ]
   }
 ];


 var MyVariable2 = [ { 
     ThingsToGet: [ etc, etc, etc...

I plan on querying it with PHP:

$variable_to_send_to_jquery = "MyVariable1";

$filter = array(
    'var'=>"MyVariable1";
);


$results = $collection->findone($filter);
4
  • Is this a one time thing or something you routinely want to do? Commented Sep 1, 2013 at 16:20
  • It seems practical for this project even though it's a little strange, if that's what you mean. There are tens of thousands of these variables. Commented Sep 1, 2013 at 16:28
  • No, do you need to insert using JSON syntax frequently? If not, I'd suggest you use Node.JS to insert the data in it's JSON form and then use PHP for your queries. Commented Sep 1, 2013 at 16:32
  • I only need to insert it once but I need to query it frequently. I just need Mongo to store the variables and not allow it to be downloaded all at once. I'm not sure about the syntax that would make it possible to pass the query along to jquery as a javascript variable. Commented Sep 1, 2013 at 16:40

1 Answer 1

1

I'd suggest if this is a one time thing that you get Node.JS installed and use code like follows if you've got a lot of pre-existing JSON-like files to load (the syntax you have is actually JavaScript). While I don't understand your data model (and changed it a tiny bit so it would compile), basically all I've done is to create one variable that contains an array of all of the "variables" that you add.

By putting them in a single array, you can then call the insert method of MongoDB to insert a large batch of documents at one time. The callback is the documents (with their _id fields set). If you have thousands of documents, you may want to use JavaScript's array slice to break the array into smaller chunks so that you don't cause a driver timeout with a really large insert.

var mongo = require('mongodb').MongoClient
    , Server = require('mongodb').Server;

var allVariables = [ {
    ThingsToGet: [
        {
            first_thing: "SOMETHING ONE",
            second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, "x"], [6, "x"]],
            third_thing: 0,
            fourth_thing: []
        },{
            first_thing: "SOMETHING TWO",
            second_thing: [[1, 0], [2, 5], [3, 4], [4, 4], [5, "x"], [6, "x"]],
            third_thing: 0,
            fourth_thing: []
        },{
            first_thing: "SOMETHING THREE",
            second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, 2], [6, 2]],
            third_thing: 0,
            fourth_thing: []
        },
    ]
}
,
    {
        ThingsToGet: [
            {
                first_thing: "SOMETHING ONE",
                second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, "x"], [6, "x"]],
                third_thing: 0,
                fourth_thing: []
            },{
                first_thing: "SOMETHING TWO",
                second_thing: [[1, 0], [2, 5], [3, 4], [4, 4], [5, "x"], [6, "x"]],
                third_thing: 0,
                fourth_thing: []
            },{
                first_thing: "SOMETHING THREE",
                second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, 2], [6, 2]],
                third_thing: 0,
                fourth_thing: []
            },
        ]
    }
];


mongo.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
    if(err) throw err;
    var collection = db.collection('variables');

    // you may want to do them in batches rather than all at once
    collection.insert(allVariables, function(err,docs){
        if(err) throw err;
        if(docs) {
            console.log("Docs inserted: " + docs.length)
            // you could do something here with the docs
        }
    });
});

The other option would be to actually use the json-decode (docs) functionality in PHP to load the JSON, and then insert the documents using the MongoDB PHP driver. You'll need to change the syntax of your JSON files to be pure JSON. For example, it can't have variable declarations in the file.

It might go something like this:

$bigJsonFile = file_get_contents('./variables.json', false)
$bigJsonObject = json_decode($json)
$collection->insert($bigJsonObject)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.