Mongodb with nodejs

home

Contents

First Look, July 8, 2017


relation db mongodb
data structure tabular format hierarchy format - like xml
whole data table association
db record row document in json format
one record item column key-value pair
join 2 tables left outer join,
add additional columns if available.
combining two associations,
add additional key-value pair if available.




scope of this topic

Some Mongodb references

Install Mongodb in mac

start the mongodb server, and accessing it from mongo, shell command prompt.

Mongo Shell Methods lab

Mongo Shell Methods lab 2, Sept. 7, 2017

nodejs application skeleton --- using nodejs package mongodb

            var MongoClient = require('mongodb').MongoClient
            var url = "mongodb://localhost:27017/mydb"
            MongoClient.connect(url, function(err, db) {
                if (err) throw err;
                console.log('after connect the mongodb')
                db.close()
                console.log('after close the connection to the mongodb') 
            }) 
            
        review mongo client api methods signature
            for example:   MongoClient.connect(url, function(err, db) {...})
        
        notes:
        1. connect method is needed for mongo client.
        2. When using mongo shell command, the server and the db client are in the same network tier.
        3. A nodejs app and the db are two different network tiers.
        4. The second method argument is a callback function.
        5. This is an asynchronously call.
        6. When the db task is completed, the callback is called with data or error message.
        7. Other api, like find, findOne, remove, insert, insertOne follow the same function pattern.  
        8. This is a functional programming feature.    
            

1: Collection - remove

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/mydb";
    MongoClient.connect(url, function(err, db) {

        if (err) throw err;
        // action 1
        db.collection("tablexyz").remove() {
        if (err) throw err;
        console.log("tablexyz is removed.");
        })

        // other actions

        db.close()     // close db
    })               // end of connect
            

2: Collection - insert one document

    // insert the following code after remove

    // 2. insert one document into a collection
    var myobj = { property_id: 17071501, address: "Green ave 37, Canton, MA" };
    db.collection("tablexyz").insertOne(myobj, function(err, res) {
        if (err) throw err
        console.log("1 record inserted");
    })  // end of insertOne
            

3: Collection - insert many document

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

        MongoClient.connect(url, function(err, db) {
              if (err) throw err;
      
              // remove
              db.collection("propertyList").remove() // delete all rows

              // insert many documents
              var myobjects = 
              [
                { property_id: 17071601, address: "Jordon St 11, f1" },
                { property_id: 17071602, address: "Jordon St 11, f2" },
                { property_id: 17071603, address: "Jordon St 12, f1" },
                { property_id: 17071604, address: "Jordon St 12, f2" }
              ]
              db.collection("propertyList").insert(myobjects, function(err, res) {
                if (err) throw err;
                console.log("3 documents inserted")
              })  // end of insert
      
              db.close()
        })  // end of connect
            

4: Collection - get all documents

 
        db.collection("propertyList").find().toArray(function(err, result) {
            if (err) throw err
            console.log(result)               // all documents in json array
            console.log(result[0])            // one document from one array item
            console.log(result[1].address)    // one item's property
        }) 
            

5: Collection - sort

  
         db.collection("propertyList").find()
                                      .sort({property_id: 1})
                                      .toArray(function(err, result) {
            if (err) throw err
            console.log(result) 
         }) 
           

5: Collection - specify fields to return, sort also

  
         db.collection("propertyList").find( {}, {address: 1, _id: 0)
                                      .sort({property_id: 1})
                                      .toArray(function(err, result) {
            if (err) throw err
            console.log(result) 
         }) 
           

7. Collections join

        // code to insert table property -------------------
        var myobjects = 
            [
                { id: 17071604, price: 300000, status: "sales pending" },
                { id: 17071603, price: 350000, status: "on sale"},
                { id: 17071601, price: 320000, status: "sold"}
            ]
            db.collection("properties").insert(myobjects, function(...){...}

        // application js code  -----------------------------
        db.collection('propertyList').aggregate(
            [
                { 
                     
                    $lookup:     // nodejs operator
                        {
                            from: 'properties',
                            localField: 'property_id',
                            foreignField: 'id',
                            as: 'property_details'
                        }
                }
            ], 
            function(err, res) {        //join result
                if (err) throw err
                console.log(res)           // output the result for study purpose

                // loop result array for study purpose
                for (var i = 0; i < res.length; i++){
                    // get one propertydata
                    var pdata = res[i].property_details[0]
                    if (pdata){
                        var s = res[i].property_details[0].status
                        var p = res[i].property_details[0].price
                        console.log("status = " + s + ", price = " + p)
                    } 
                }
                
                1. If you see the test result, property document is [[Object]] or [].
                   That is why I use loop to examine the data.
                   I believe that in real code, you can wait until you try to use the join result.
                2. For the type of table property with [[  ]], I follow the sample code.
                   When you try to get data, simply follow the model.
                
                
            } // end of function
        }     // end of aggregate 


        // test result  ----------------------------------------
        [ { _id: 596bd0e6c6cbd2ef6d21d16a,
            property_id: 17071604,
            address: 'Jordon St 12, f2',
            property_details: [ [Object] ] },  // from the 2nd table
          { _id: 596bd0e6c6cbd2ef6d21d16b,
            property_id: 17071603,
            address: 'Jordon St 12, f1',
            property_details: [ [Object] ] },  // from the 2nd table
          { _id: 596bd0e6c6cbd2ef6d21d16c,
            property_id: 17071602,
            address: 'Jordon St 11, f2',
            property_details: [] },            // no data from the 2nd table
          { _id: 596bd0e6c6cbd2ef6d21d16d,
            property_id: 17071601,
            address: 'Jordon St 11, f1',
            property_details: [ [Object] ] } ]  // from the 2nd table

         status =  sales pending, price = 300000
         status =  on sale,       price = 350000
         status =  sold,          price = 320000
            

8: One document - get, update, delete

 
        // 1. get one ducument find(query, projection) from w3c-nodejs-mongodb
        var query = { id: 17071604 }
        db.collection("properties").find(query).toArray(function(err, result) {
            if (err) throw err
            console.log("result = " + result) // [object object]
            console.log("price = " + result[0].price) // 300000
        }) // end toArray

        // 2. update one document, updateOne(filter, update,options)
        // from mongodb web site
        // $set is nodejs operator
        // verify the result, using find method
        try {
            db.collection('properties').updateOne(      
                                        { id : 17071604 },
                                        { $set: { price : 299000 } }
            ) // end of updateOne
            console.log("one document updated")
        } catch (e) {
            print(e)
        } 

        // 3. delete one document, deleteOne(filter,...)
        // from mongodb web site
        // $eq is nodejs operator
        // verify the result, using find method, the document(status is sold) is removed. no longer needed.
        try {
            db.collection("properties").deleteOne( { "status" : { $eq: 'sold' } } )
            console.log("one document deleted")
        } catch (e) {
            print(e)
        }