Posts Issued in October, 2014

General notes on this course will write here. This course seems to be more difficult than MongoDB for devs,at least 3 hours of lectures vs 1 hour is a great difference!

  • Week1
  • Week2

MongoImport works as follows:

serhii@serhii:~/Downloads$ mongoimport --db pcat --collection products < products.json

Homework 1.4: How would you print out, in the shell, the name of all the products without extraneous characters or braces, sorted alphabetically, ascending? (Check all that would apply.)

db.products.find({},{name:1,_id:0}).sort({name:1}) 
//output
{ "name" : "AC3 Case Black" }
{ "name" : "AC3 Case Green" }
{ "name" : "AC3 Case Red" }
{ "name" : "AC3 Phone" }
{ "name" : "AC3 Series Charger" }
{ "name" : "AC7 Phone" }
{ "name" : "Cable TV Basic Service Package" }
{ "name" : "Phone Extended Warranty" }
{ "name" : "Phone Service Basic Plan" }
{ "name" : "Phone Service Core Plan" }
{ "name" : "Phone Service Family Plan" }
 
var c = db.products.find({},{name:1,_id:0}).sort({name:1}); while( c.hasNext() ) print( c.next().name); 
//reslut
AC3 Case Black
AC3 Case Green
AC3 Case Red
AC3 Phone
AC3 Series Charger
AC7 Phone
Cable TV Basic Service Package
Phone Extended Warranty
Phone Service Basic Plan
Phone Service Core Plan
Phone Service Family Plan
 
var c = db.products.find({}).sort({name:1}); c.forEach( function(doc){ print(doc.name) } ); 
//res
AC3 Case Black
AC3 Case Green
AC3 Case Red
AC3 Phone
AC3 Series Charger
AC7 Phone
Cable TV Basic Service Package
Phone Extended Warranty
Phone Service Basic Plan
Phone Service Core Plan
Phone Service Family Plan
 
var c = db.products.find({}).sort({name:-1}); while( c.hasNext() ) print( c.next().name);
//res
AC3 Case Black
AC3 Case Green
AC3 Case Red
AC3 Phone
AC3 Series Charger
AC7 Phone
Cable TV Basic Service Package
Phone Extended Warranty
Phone Service Basic Plan
Phone Service Core Plan
Phone Service Family Plan

Heading 4

var t = db.products;
t.save
//outputs
function ( obj , opts ){
    if ( obj == null )
        throw "can't save a null";
 
    if ( typeof( obj ) == "number" || typeof( obj) == "string" )
        throw "can't save a number or string"
 
    if ( typeof( obj._id ) == "undefined" ){
        obj._id = new ObjectId();
        return this.insert( obj , opts );
    }
    else {
        return this.update( { _id : obj._id } , obj , Object.merge({ upsert:true }, opts));
    }
}
//that means Function called if update doc like
var x = db.products.find({_id : ObjectId("507d95d5719dbef170f15c00")});
x.term_years = 3;
t.update({_id : x._id}, x);
//same result
t.save(x);
 
//How many products have a voice limit? (That is, have a voice field present in the limits array.)
db.products.find( { limits: { $exists: [ "voice"] } } ).count();
 
/*Quiz: Partial Updates & Document Limits The Mongo Web Shell has been initialized with one collection, cars, with one document preexisting:
{ "_id" : 100, "name" : "GTO", "year" : 1969, "color" : "red" }
Set the available field to 1. 
*/
db.cars.update({_id: 100}, {$set: {available: 1}});
 
//will romove "hello", "hellos, etc
db.cars.remove({ x: /ell/});

Privacy notes

Privacy notes