Search code examples
node.jsdatabasemongodbherokumlab

Copied MongoDb from cloud9 to Mlab and new documents are saved in odd order to DB


I am trying to use databases on a website for the first time and running into something odd. I did a webDev course that showed how to create a blog type site that use cloud9 to do all the development, and then deployed the finished site to heroku using a MongoDb databse and using Mlab to host the database once you have the site on heroku.

Everything works out great, however I added a bunch of collections and documents to the database while it was still on cloud9 and the instructor in the webDev course never went into how to move this database to Mlab. I found some info and used the mongodump command to save out the entire database and on mlab found this code, I used this basic command to get the mongodump onto mlab:

  mongorestore -h ds153637.mlab.com:53637 -d nttw_db -c travels -u <user> -p <password> travels.bson

The database seems to have transferred fine, and my site when testing it on heroku, seems to have all the data I initially stored in the DB when it was created on cloud9. The problem I run into is now when I add to the database on the live deployed site hosted on heroku, database entries are being added to the collections in odd orders on Mlab. This is a blog type site with hiking trails and campgrounds and reviews on gear, so when i added a new test entry to the trails, the new document was added after the first entry in the collection, but before the other 4 entries. Usually the new data I enter gets appended to the end of the collection, at least that's how it worked when I was adding entries when the DB was on cloud9. Here is the code that is used in my app.js on the server to add a new entry posting from the form on my site:

router.post("/trail_new", isLoggedIn, function(req, res){

    var name = req.body.trail.name;
    var tags = req.body.trail.tags;
    var splash = req.body.trail.splashText;

    var thl     = req.body.trail.thl || "somewhere on earth.!";
    var fees    = req.body.trail.fees;
    var tl      = req.body.trail.tl;
    var info    = req.body.trail.info;

    var body = req.body.trail.body;
    var adRight = req.body.trail.adRight || "none";
    var photos = req.body.reviewPhoto;

    tags = tags.split(",");

    //Assemble a variable to send all the review data into the create function for the SCHEMA pattern
    var newTrail = { name: name, tags: tags, splashText: splash, thl: thl, fees: fees, tl: tl, info: info, body: body, adRight: adRight, photos: photos };

    Trail.create( newTrail, function(err, travel){ 
        if(err){
            console.log(err);
            res.redirect("trail/new");
        }else{
            console.log(travel);
            res.redirect("/trail");
        }
    });
});

I am sure there are some less-than-elegant lines in the above code, but it all appears to work fine for adding entries into the database. Is there any thing I am overlooking about how Mlab adds to the database.? I don't know why new entries are being inserted into the db after the first entry in each collection and before the other ones that were all there before copying the mongodump over to Mlab. The version of mongo running on the cloud9 development environment is v3.6.9 and the Mlab version is 3.6.6 (MMAPv1). Not sure if this matters or not.

I would really hate to have to recreate all the entires I have already made in the database from when it was on cloud9, but If I have to wipe the database and start over with it on Mlab, I can do it. It may not even be an issue, but It worries me slightly that the new entries are not being put into the DB in the correct order and don't want it to somehow corrupt the database down the road if there is an issue that I don't know about.

Any information would be great. This is the first attempt at using databases and they are so cool, but confusing for a first timer.!

Thanks

E


Solution

  • The key here that you need to know IMO is that in MongoDB, the data is stored as a big JSON blob. And JSON by definition is a loosely bounded blob of key-value pairs. The order of objects doesn't matter in JSON and you should keep this in mind. So when you dump your data in C9 & upload in Mlab, neither one cares about the order of JSON objects and you see the order messed up. Think of this particular point as a slight drawback of using the NOSQL databases and move on.

    P.S Did you take the Udemy Bootcamp course by Steele?