DevTech101

DevTech101

Configuring MongoDB

First test and connect to mongo
From connect line

> use mcfly
switched to db mcfly
> db.createCollection('mathpeeps')
{ "ok" : 1 }
> db.mathpeeps.insert({Firstname: 'Eli', lastname: 'Kleinman'})
WriteResult({ "nInserted" : 1 })
> db.mathpeeps.insert({Firstname: 'Moshe', lastname: 'Kleinman'})
WriteResult({ "nInserted" : 1 })
> db.mathpeeps.find()
{ "_id" : ObjectId("55ce19e7737e49e3a5a7553e"), "Firstname" : "Eli", "lastname" : "Kleinman" }
{ "_id" : ObjectId("55ce19eb737e49e3a5a7553f"), "Firstname" : "Moshe", "lastname" : "Kleinman" }
> 
bye

Using NodeJS and mongodb driver

npm install mongodb

Creating the javascript file fro mongodb driver

var mongodb = require('mongodb');

var db = new mongodb.Db('mcfly', new mongodb.Server('127.0.0.1', 27017), {safe: true});

db.open(function (err) {
    db.collection('mathpeeps', function (err, collection) {
        /*
        // Lookup all item
         collection.find().toArray(function(err, items) {
         console.dir(items);
         //to exit after search
         process.exit();
         });
         // Lookup single item
         collection.findOne({lastname: 'Kleinman'}, function (err, item) {
         console.log('found the item as:');
         console.dir(item);        
         process.exit();
         });
         */
        // Update record 
        collection.update({lastname: 'Kleinman'}, {$set: {middlename: 'gimpple'}}, function (err) {
        });
        collection.findOne({lastname: 'Kleinman'}, function (err, item) {
            console.log('found the item as:');
            console.dir(item);
            process.exit();
        });
    });
});
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: