You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
257 lines
4.7 KiB
JavaScript
257 lines
4.7 KiB
JavaScript
1 year ago
|
// CRUD Operations
|
||
|
|
||
|
/*
|
||
|
- CRUD Operations are the heart of any backend application.
|
||
|
- Mastering the CRUD operations is essential for any developer
|
||
|
- CRUD stands for CREATE, READ, UPDATE, and DELETE
|
||
|
*/
|
||
|
|
||
|
|
||
|
// [SECTION] Inserting documents (Create)
|
||
|
|
||
|
// Insert One Document
|
||
|
/*
|
||
|
Syntax:
|
||
|
db.collectionName.insertOne({object});
|
||
|
*/
|
||
|
|
||
|
db.users.insertOne({
|
||
|
firstName: "Jane",
|
||
|
lastName: "Doe",
|
||
|
age: 21,
|
||
|
contact: {
|
||
|
phone: "87654321",
|
||
|
email: "janedoe@gmail.com"
|
||
|
},
|
||
|
course: ["CSS", "JavaScript", "Python"],
|
||
|
department: "none"
|
||
|
});
|
||
|
|
||
|
// Insert Many
|
||
|
/*
|
||
|
Syntax:
|
||
|
db.collectionName.insertMany([{Object A}, {Object B}]);
|
||
|
*/
|
||
|
|
||
|
db.users.insertMany([
|
||
|
{
|
||
|
firstName: "Stephen",
|
||
|
lastName: "Hawking",
|
||
|
age: 79,
|
||
|
contact: {
|
||
|
phone: "87654321",
|
||
|
email: "stephenhawking@gmail.com"
|
||
|
},
|
||
|
course: ["Python", "React", "PHP"],
|
||
|
department: "none"
|
||
|
},
|
||
|
{
|
||
|
firstName: "Neil",
|
||
|
lastName: "Armstrong",
|
||
|
age: 82,
|
||
|
contact: {
|
||
|
phone: "87654321",
|
||
|
email: "neilarmstrong@gmail.com"
|
||
|
},
|
||
|
course: ["React", "Laravel", "Sass"],
|
||
|
department: "none"
|
||
|
}
|
||
|
|
||
|
]);
|
||
|
|
||
|
// [SECTION] Finding Documents (Read)
|
||
|
|
||
|
// Find Single Document
|
||
|
/*
|
||
|
Syntax:
|
||
|
db.collectionName.findOne();
|
||
|
db.collectionName.findOne({ field: value });
|
||
|
*/
|
||
|
|
||
|
// Leaving the search criteria empty will retrieve ALL the documents
|
||
|
db.users.find();
|
||
|
|
||
|
// This will retrieve the first document
|
||
|
db.users.findOne();
|
||
|
// criteria
|
||
|
db.users.findOne({ firstName: "Stephen" });
|
||
|
|
||
|
// Finding Multiple Documents
|
||
|
/*
|
||
|
Syntax:
|
||
|
db.collectionName.find({ fieldA: valueA });
|
||
|
*/
|
||
|
|
||
|
db.users.find({ department: "none" });
|
||
|
|
||
|
// Finding multiple documents with multiple parameters (criteria)
|
||
|
db.users.find({ department: "none", age: 82 });
|
||
|
|
||
|
// [SECTION] Updating Documents (Update)
|
||
|
|
||
|
// Updating a single document
|
||
|
|
||
|
// Creating document to update
|
||
|
db.users.insertOne({
|
||
|
firstName: "Test",
|
||
|
lastName: "Test",
|
||
|
age: 0,
|
||
|
contact: {
|
||
|
phone: "000000",
|
||
|
email: "test@gmail.com"
|
||
|
},
|
||
|
courses: [],
|
||
|
department: "none"
|
||
|
});
|
||
|
|
||
|
|
||
|
/*
|
||
|
- Just like the "find" methos, methods that only manipulate a sigle document will only manipulate a single document will only update the FIST document that matches the search criteria
|
||
|
- Syntax:
|
||
|
db.collectionName.updateOne({criteria}, {$set: {field: value}});
|
||
|
*/
|
||
|
|
||
|
db.users.updateOne({ firstName: "Test" }, {
|
||
|
$set: {
|
||
|
firstName: "Bill",
|
||
|
lastName: "Gates",
|
||
|
age: 65,
|
||
|
contact: {
|
||
|
phone: "12345678",
|
||
|
email: "bill@gmail.com"
|
||
|
},
|
||
|
courses: ["PHP", "Laravel", "HTML"],
|
||
|
department: "Operations",
|
||
|
status: "active"
|
||
|
}
|
||
|
});
|
||
|
|
||
|
db.users.find({ firstName: "Bill" });
|
||
|
|
||
|
// Another Example using _id
|
||
|
db.users.updateOne({ _id: ObjectId("6514da527c5d7043df600f54") }, {
|
||
|
$set: {
|
||
|
firstName: "Elon",
|
||
|
lastName: "Musk",
|
||
|
age: 65,
|
||
|
contact: {
|
||
|
phone: "12345678",
|
||
|
email: "elon@gmail.com"
|
||
|
},
|
||
|
courses: ["PHP", "Laravel", "HTML"],
|
||
|
department: "Operations",
|
||
|
status: "active"
|
||
|
}
|
||
|
});
|
||
|
|
||
|
db.users.updateOne({ _id: ObjectId("6514da527c5d7043df600f54") }, {
|
||
|
$set: {
|
||
|
firstName: "Bill",
|
||
|
lastName: "Gates"
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Updating multiple documents
|
||
|
/*
|
||
|
Syntax:
|
||
|
db.collectionName.updateMany({ criteria }, {$set: { field: value }});
|
||
|
*/
|
||
|
|
||
|
db.users.updateMany({department: "none"}, {
|
||
|
$set: {
|
||
|
department: "HR"
|
||
|
}
|
||
|
});
|
||
|
|
||
|
db.users.find();
|
||
|
|
||
|
|
||
|
// Replace One
|
||
|
/*
|
||
|
- Can be used if replacing the whole document is necessary.
|
||
|
- Syntax:
|
||
|
db.collectionName.replaceOne( { criteria }, { field: value } );
|
||
|
*/
|
||
|
|
||
|
db.users.replaceOne({firstName: "Bill"}, {
|
||
|
|
||
|
firstName: "Bill",
|
||
|
lastName: "Gates",
|
||
|
age: 70,
|
||
|
contact: {
|
||
|
phone: "123123123",
|
||
|
email: "billgates@gmail.com"
|
||
|
},
|
||
|
courses: ["PHP", "Laravel", "CSS"],
|
||
|
department: "Operations"
|
||
|
|
||
|
});
|
||
|
|
||
|
// [SECTION] Deleting Documents (Delete)
|
||
|
|
||
|
// Create a document to delete
|
||
|
db.users.insert({
|
||
|
firstName: "test"
|
||
|
});
|
||
|
|
||
|
// Deleting a single Document
|
||
|
/*
|
||
|
Syntax:
|
||
|
db.collectionName.deleteOne({criteria});
|
||
|
*/
|
||
|
db.users.deleteOne({
|
||
|
firstName: "test"
|
||
|
});
|
||
|
|
||
|
db.users.find();
|
||
|
|
||
|
// Delete Many
|
||
|
/*
|
||
|
|
||
|
DO NOT USE: db.collectionName.deleteMany();
|
||
|
|
||
|
Syntax:
|
||
|
db.collectionName.deleteMany({criteria});
|
||
|
*/
|
||
|
|
||
|
db.users.deleteMany({
|
||
|
firstName: "Bill"
|
||
|
});
|
||
|
|
||
|
db.users.find();
|
||
|
|
||
|
// TAKE HOME QUIZ
|
||
|
|
||
|
// [SECTION] Advanced Queries
|
||
|
|
||
|
// 1. Query an embedded document
|
||
|
/*
|
||
|
- find all the document with the following contact:
|
||
|
phone: "87654321",
|
||
|
email: "stephenhawking@gmail.com"
|
||
|
*/
|
||
|
|
||
|
// 2. Query on nested Field
|
||
|
/*
|
||
|
- find the document that has an email of "janedoe@gmail.com"
|
||
|
*/
|
||
|
|
||
|
// 3. Query an Array with Exact Elements
|
||
|
/*
|
||
|
- find a document with the has the courses
|
||
|
- CSS
|
||
|
- JavaScript
|
||
|
- Python
|
||
|
|
||
|
*/
|
||
|
|
||
|
// 4. Querying an array without regard to order
|
||
|
/*
|
||
|
- find all the documents with the following courses
|
||
|
- React
|
||
|
- Python
|
||
|
*/
|
||
|
|
||
|
|
||
|
|