Kamis, 13 Maret 2014

Mongo M101JS Week 2 CRUD

,
CRUD

Create - save
Retrieve - find
Update - update
Delete - remove



db.runCommand( { getLastError : 1 } )  // returns the result of last operation
> db.ALERT_TABLE.count()
0
> db.runCommand({getLastError:1})

{ "n" : 0, "connectionId" : 136, "err" : null, "ok" : 1 }
The err field would contain an error message had this been an error condition.
When err is null that is an indication that the operation succeeded.


db.people.update( {}, { $set : {title: "Dr"} }, { multi: true} ) 



var MongoClient = require( mongodb ).MongoClient;

console.dir(Checking findOne with Node.js);
var testFindOne=function() {
    console.dir(testFindOne);
    MongoClient.connect(mongodb://localhost:27017/course, function(err, db) {
                    if (err) throw err;
                    var query = {grade : 100 };
                    
                    db.collection(grades).findOne(query, function(err,doc) {
                                                    if(err) throw err;
                                                    console.dir(doc);
                                                    db.close();
                                                    });
                    }
                    

                    
);
};
var testFind=function() {
    console.dir(testFind);
    MongoClient.connect(mongodb://localhost:27017/course, function(err, db) {
                        if (err) throw err;
                        var query = {grade : 100 };
                        db.collection(grades).find(query).toArray(function(err,docs) {
                                                                    if (err) throw err;
                                                                    console.dir(docs);
                                                                    db.close();
                                                                    
                                                                    });
                        }
                        
                        
                        );
    
};
var testFindWithCursor=function() {
    console.dir(testFind);
    MongoClient.connect(mongodb://localhost:27017/course, function(err, db) {
                        if (err) throw err;
                        var query = {grade : 100 };
                        var cursor=db.collection(grades).find(query);
                        cursor.each(function(err,doc) {
                                    if (err) throw err;
                                    if (doc == null) {
                                        return db.close();
                                    }
                                    console.dir(doc.student + " got a good grade!");
                                    });
                        }
                        
                        
                        );
};
var testFindWithProjection=function() {
    console.dir(testFindWithProjection);
    MongoClient.connect(mongodb://localhost:27017/course, function(err, db) {
                        if (err) throw err;
                        var query = {grade : 100 };
                        var projection = {student:1,_id:0};
                        var cursor=db.collection(grades).find(query,projection);
                        cursor.each(function(err,doc) {
                                    if (err) throw err;
                                    if (doc == null) {
                                    return db.close();
                                    }
                                    console.dir(doc);
                                    //console.dir(doc.student + " got a good grade!");
                                    });
                        }
                        
                        
                        );
    
};


// testFindOne();
// testFind();
// testFindWithCursor();
testFindWithProjection();

$ node findOneWithNodeJsDriver.js 

Checking findOne with Node.js
{ _id: 52e3d5b6fb88577ec1496219,
  student: Joe,
  assignment: exam,
  grade: 100 }


Importing from REDDIT (www.reddit.com/r/technology/.json)

var MongoClient = require(mongodb).MongoClient, request = require(request);

MongoClient.connect(mongodb://localhost:27017/course,function(err,db) {
                    if (err) throw err;
                    request(http://www.reddit.com/r/technology/.json, function(error, response, body) {
                            if (!error && response.statusCode == 200) {
                            var obj = JSON.parse(body);
                            
                            var stories = obj.data.children.map(function(story) {return story.data; }
                                                                );
                            db.collection(reddit).insert(stories, function(err,data) {
                                                           if(err) throw err;
                                                           console.dir(data);
                                                           db.close();
                                                           
                                                           });
                            }
                            });
                    });

----

var MongoClient = require(mongodb).MongoClient;

MongoClient.connect(mongodb://localhost:27017/course, function(err, db) {
    if(err) throw err;

    var query = { title : { $regex : Microsoft } };

    var projection = { title : 1, _id : 0 };

    db.collection(reddit).find(query, projection).each(function(err, doc) {
        if(err) throw err;

        if(doc == null) {
            return db.close();
        }

        console.dir(doc.title);
    });
});

Dot Notation:

var MongoClient = require(mongodb).MongoClient;
var request = require(request);

var insertReddit=function(page,collection) {
    MongoClient.connect(mongodb://localhost:27017/course, function(err, db) {
                        if (err) {
                            console.dir("FUNCKSTER");
                           throw err;
                        }
                        var url=http://www.reddit.com/+page+.json;
                        console.dir("url="+url);
                        console.dir("request is next");
                        request(url,function(error,response,body) {
                                if(!error && response.statusCode == 200) {
                                console.dir(good neews);
                                var obj = JSON.parse(body);
//                                console.dir(obj);
                                var stuff = obj.data.children.map(function(datum) {
                                                                  return datum;
                                                                  });
                                //console.dir(stuff);
                                db.collection(collection).insert(stuff,function(err,data) {
                                                                 if(err) throw err;
                                                                 console.dir(data);
                                                                 db.close();
                                                                 });
                                
                                }
                                });
                        /*
                         */
                        });
};


MongoClient.connect(mongodb://localhost:27017/course, function(err, db) {
    if(err) throw err;

    var query = { media.oembed.type : video };

    var projection = { media.oembed.url : 1, _id : 0 };

    db.collection(reddit_front).find(query, projection).each(function(err, doc) {
        if(err) throw err;

        if(doc == null) {
            return db.close();
        }

        console.dir(doc);
    });
});

insertReddit(,reddit_front);


0 komentar to “Mongo M101JS Week 2 CRUD”

Posting Komentar

 

Our Information Copyright © 2016-2022 -- Powered by Blogger