CS331 - Database Management Systems


Dr. Lilia Sfaxi


13 novembre 2019


Mongo DB


To get a docker container with mongodb :


docker pull mongo


docker run --name medtech-mongo -d mongo:latest


docker run -it --link medtech-mongo:mongo --rm mongo mongo --host mongo test


Use the database


use medtech-db


to see all databases:


show dbs


insert document:


db.blog.insert({"name":"tutorials point"})


now show dbs will display the database medtech-db


create a new collection


db.createCollection("mycollection")


show collections


drop a collection


db.mycollection.drop()


insert a document:


db.blog.insert([


	
{


	
title: 'MongoDB Overview',


MONGO DB DR. LILIA SFAXI 1
MedTech
description: 'MongoDB is no sql database',


	
by: 'tutorials point',


	
url: 'http://www.tutorialspoint.com',


	
tags: ['mongodb', 'database', 'NoSQL'],


	
likes: 100


	
},


	
	


	
{


	
title: 'NoSQL Database',


	
description: "NoSQL database doesn't have tables",


	
by: 'tutorials point',


	
url: 'http://www.tutorialspoint.com',


	
tags: ['mongodb', 'database', 'NoSQL'],


	
likes: 20,


	
comments: [
	


	
{


	
user:'user1',


	
message: 'My
fi
rst comment',


	
dateCreated: new Date(2013,11,10,2,35),


	
like: 0


	
}


	
]


	
}


	
])


to query a collection:


db.blog.
fi
nd()


you can add a pretty method:


db.blog.
fi
nd().pretty()


you can use
fi
ndOne to return one document (the
fi
rst)


db.blog.
fi
ndOne()


search an equality


MONGO DB DR. LILIA SFAXI 2
db.blog.
fi
nd({"by":"tutorials point"}).pretty()


Less than


db.blog.
fi
nd({"likes":{$lt:50}}).pretty()


Greater than


db.blog.
fi
nd({"likes":{$gt:50}}).pretty()


db.blog.
fi
nd({"likes":{$gte:50}}).pretty()


Not equals


db.blog.
fi
nd({"likes":{$ne:50}}).pretty()


AND


db.blog.
fi
nd({$and:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty()


OR


db.blog.
fi
nd({$or:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty()


AND and OR together


db.blog.
fi
nd({"likes": {$gt:10}, $or: [{"by": "tutorials point"},


{"title": "MongoDB Overview"}]}).pretty()


update


db.blog.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})


update for all docs


db.blog.update({'title':'MongoDB Overview'},


{$set:{'title':'New MongoDB Tutorial'}},{multi:true})


search with a projection


db.blog.
fi
nd({},{"title":1,_id:0})


sort (descending title)


db.blog.
fi
nd({},{"title":1,_id:0}).sort({"title":-1})


MONGO DB DR. LILIA SFAXI 3
add index


db.blog.ensureIndex({"title":1})
MONGO DB DR. LILIA SFAXI 4

Lab2-DB-Mongodb

  • 1.
    CS331 - DatabaseManagement Systems Dr. Lilia Sfaxi 13 novembre 2019 Mongo DB To get a docker container with mongodb : docker pull mongo docker run --name medtech-mongo -d mongo:latest docker run -it --link medtech-mongo:mongo --rm mongo mongo --host mongo test Use the database use medtech-db to see all databases: show dbs insert document: db.blog.insert({"name":"tutorials point"}) now show dbs will display the database medtech-db create a new collection db.createCollection("mycollection") show collections drop a collection db.mycollection.drop() insert a document: db.blog.insert([ { title: 'MongoDB Overview', MONGO DB DR. LILIA SFAXI 1 MedTech
  • 2.
    description: 'MongoDB isno sql database', by: 'tutorials point', url: 'http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }, { title: 'NoSQL Database', description: "NoSQL database doesn't have tables", by: 'tutorials point', url: 'http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 20, comments: [ { user:'user1', message: 'My fi rst comment', dateCreated: new Date(2013,11,10,2,35), like: 0 } ] } ]) to query a collection: db.blog. fi nd() you can add a pretty method: db.blog. fi nd().pretty() you can use fi ndOne to return one document (the fi rst) db.blog. fi ndOne() search an equality MONGO DB DR. LILIA SFAXI 2
  • 3.
    db.blog. fi nd({"by":"tutorials point"}).pretty() Less than db.blog. fi nd({"likes":{$lt:50}}).pretty() Greaterthan db.blog. fi nd({"likes":{$gt:50}}).pretty() db.blog. fi nd({"likes":{$gte:50}}).pretty() Not equals db.blog. fi nd({"likes":{$ne:50}}).pretty() AND db.blog. fi nd({$and:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty() OR db.blog. fi nd({$or:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty() AND and OR together db.blog. fi nd({"likes": {$gt:10}, $or: [{"by": "tutorials point"}, {"title": "MongoDB Overview"}]}).pretty() update db.blog.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}}) update for all docs db.blog.update({'title':'MongoDB Overview'}, {$set:{'title':'New MongoDB Tutorial'}},{multi:true}) search with a projection db.blog. fi nd({},{"title":1,_id:0}) sort (descending title) db.blog. fi nd({},{"title":1,_id:0}).sort({"title":-1}) MONGO DB DR. LILIA SFAXI 3
  • 4.