Dr. Andrew Besmer
mongo deltona.birdnest.org/csci355csci355 Pass: csci355db.auth('csci355', 'csci355');$ mongo deltona.birdnest.org/group1
> db.auth('group1', 'group1');
| Doc Oriented | RDBMS |
|---|---|
| Collection | Table |
| Document | Row |
| Property | Column |
use a database
use MyDatabaseName;use command assigns the specified database to a variable dbdb to see which you are usingusers collectiondb//Assign the variable db to the university database
use university;
/*
* Find all the documents in the
* university databases collection
* users
*/
db.users.find()
db.users.find() - Get all the documents in the users collectionname - Name of the personage - Age of the personstatus - Current gradegroups - List of groups the person belongs to{
name: "Fred Duncan", // <- field: value
age: 22, // <- field: value
status: "A", // <- field: value
groups: [ "students", "employees" ] // <- field: value
}
groups would violate 1NF and be considered bad design{
name: "Fred Duncan", // <- field: value
age: 22, // <- field: value
status: "A", // <- field: value
groups: [ "students", "employees" ] // <- field: value
}
{
name: {
first: "Sue",
last: "Person"
},
age: 28,
categories: [ {name : "students"}, {name: "honors"} ]
}
$ or .use and inserting some data in a collection4show dbs to see which currently existdb.users.insert({name:"Sue Person"}); inserts a document and creates the collection if it doesn’t existdb.createCollection() can be used to create the collection without inserting datashow collections; or db.getCollectionNames(); to see a listdb.createCollection() allows for custom collection creation
size is bytesmax is ndb.createCollection(name, {
capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>
});
db.collection.insert()//Insert into the users collection
db.users.insert (
{
name: "Sue Person",
age: 26,
status: "A",
groups: [ "students", "honors" ]
}
)
Double, String, ArrayBinary, ObjectId, BooleanDate, Integer, and so onObjectId however is interesting, lets take a lookObjectId is set automatically by mongo if you don’t provide _id
ObjectId made up of 12 bytesdb.collection.find()db.users.find() to see that they worked!db.users.insert({name:"Alice"});
db.users.insert({name:"Bob"});
db.users.insert({name:"Trudy"});
db.users.find();
db.collection.find()db.collection.aggregate()db.users.find();
db.users.aggregate();
It is possible to do criteria based selection
$gt - Greater than$gte - Greater than or equal to$lt - Less than$lte - Less than or equal to$ne - Not equal toThese all work conceptually the same way they do in MySQL or programming
//drop the collection
//insert some sample
db.students.remove({});
db.students.insert({
name: "Alice",
age: 16, //Genius
grades: [70, 98, 88, 95]
});
db.students.insert({
name: "Alice",
age: 30, //Not Genius
grades: [27, 49, 52, 65]
});
db.students.insert({
name: "Bob",
age: 34,
grades: [67, 75, 73, 81]
});
db.students.insert({
name: "Trudy",
age: 21,
grades: [90, 98, 94, 100]
});
db.students.insert({
name: "Joe",
age: 27,
grades: [40, 77, 65, 80]
});
db.students.insert({
name: "Robson",
age: 18,
grades: [56, 82, 74, 68]
});
db.students.find({
age : { //search age
$gt: 18 // > 18
}
});
$in - Does any of the values in an array match?$nin - Does any of the values in the array not matchdb.students.find({
age : {
$in: [18, 21]
}
});
$or - logical or$and - logical and$not - opposite$nor - no match for both criteriadb.students.find({
$or : [ //an array of expressions
{
name: "Joe"
},
{
age: { $gte : 21 }
}
]
});
db.students.find({
$and : [ //an array of expressions
{
name: "Alice"
},
{
age: { $lt : 21 }
}
]
});
or
//So what does this tell you?
db.students.find({
name: "Alice",
age: { $lt : 21 }
});
$mod - Modulus$regex - Regular expression$text - Text search (requires index)$where - Custom javascript expression$geoWithin/$geoIntersects - Geolocation matching, i.e. everything within a radius!// What is interesting about these results?
db.students.find({
grades: { $gte : 80}
});
SELECT projection FROM table.find()
1 OR exclusion list 0_iddb.students.find(
{}, // Criteria, or all documents
{
name : 1 // Projection, inclusion list only the name
});
or
db.students.find(
{}, // Criteria, or all documents
{
age : 0 // Projection, exclusion list age
});
_id on or off has no effect on inclusion list vs exclusion list$ mongo deltona.birdnest.org/group1
> db.auth('group1', 'group1');
{
name: "Alice",
dorm: {
name: "Thurmond",
room: "304"
},
classes: [
{ name: "Databases", days: ["Monday", "Wednesday"], credits: 3 },
{ name: "Advanced Databases", days: ["Tuesday", "Thursday"] , credits: 3 },
]
}
db.collection.update().update() usually takes at least two parameters
_id thoughdb.students.insert({_id: 12345, name: "Gary", age: 44});
db.students.update({_id:12345}, {age:45});
db.students.find({_id:12345});
// { "_id" : 12345, "age" : 45 }
$inc - Increment (positive or negative) the field by the value$mul - Multiply the field by the value$rename - Rename the field$set - Set the field to the value$unset - Unset the field entirely (removal)$min - Set the min of existing or new value$max - Set the max of existing or new value$currentDate Set the current date/timedb.students.remove({_id:12345}); //remove old
db.students.insert({_id: 12345, name: "Gary", age: 44});
db.students.update(
{ _id : 12345 }, //what doc to update
{ $set :
{
age : 45 //set the age to 45
}
}
);
db.students.find({_id:12345});
// { "_id" : 12345, "name" : "Gary", "age" : 45 }
multiple documentsupsert and writeConcern db.students.update(
{}, //Match all documents
{ $inc: {age:1} }, //Increment the age by one
{ multi:true } //Update more than 1 document
);
$ - Symbolizes the first element in the array to match query conditions$addToSet - Add element to array but only if it doesn’t already exist$pop - Remove first (-1) or last (1) element in the array$pullAll - Remove all matching provided elements in the array$pull - Removes all elements from the array matching search criteria$push - Adds element to the end of the arraydb.students.update(
{name: "Robson"}, //What to update
{
$push : {grades: 100} //Push a 100 to grades array
}
);
db.collection.remove().remove() takes a query object that works exactly like all previous examples.update() doing a .remove() does not work on one document by defaultdb.students.remove({}); //Dang!
db.students.drop();
.dropDatabase()db.dropDatabase();
$ mongo deltona.birdnest.org/group1
> db.auth('group1', 'group1');
db.product.drop();
db.product.insert({
name: "Batman Lego Set",
qty: 1,
price: 9.99,
company: "LEGO",
category: "Toys"
});
db.product.insert({
name: "32 Piece Ratchet Set",
qty: 2,
price: 29.99,
company: "Stanley",
category: "Tools"
});
db.product.insert({
name: "Hammer",
qty: 5,
price: 5.99,
company: "Stanley",
category: "Tools"
});
db.product.insert({
name: "Toothpaste",
qty: 1,
price: 2.99,
company: "Crest",
category: "Health"
});
db.product.insert({
name: "Floss",
qty: 0,
price: .99,
company: "Crest",
category: "Health"
});
db.product.insert({
name: "Power Drill",
qty: 5,
price: 49.99,
category: "Tools"
});
db.product.insert({
name: "120 Piece Lego Set",
qty: 1,
price: 12.99,
company: "LEGO"
});
db.product.insert({
name: "Hot-wheels Car",
qty: 10,
price: .99,
company: "Matchbox"
});
4.1. Update the company “Stanley” to “Stanley Black and Decker” and re-run * How many documents needed to be updated? * How many rows would MySQL have required?