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.
128 lines
2.3 KiB
JavaScript
128 lines
2.3 KiB
JavaScript
// S34 - MongoDB CRUD Operations Activity Template:
|
|
|
|
/*
|
|
|
|
Sample solution:
|
|
|
|
async function addOneQuery(db) {
|
|
await (
|
|
|
|
//add query here
|
|
|
|
db.collectionName.insertOne({
|
|
field1: "value1",
|
|
field2: "value2"
|
|
}) //DO NOT ADD SEMICOLON.
|
|
|
|
);
|
|
|
|
return(db);
|
|
}
|
|
|
|
Note:
|
|
- Do note change the functionName or modify the exports
|
|
- Do not add semicolon after query
|
|
|
|
|
|
*/
|
|
|
|
// 1. Insert a single room (insertOne method) in the rooms collection:
|
|
|
|
async function addOneFunc(db) {
|
|
|
|
await (
|
|
db.hotel.insertMany({
|
|
name:"single",
|
|
accomodates:2,
|
|
price:1000,
|
|
description:"A simple room with all the basic neccessities",
|
|
rooms_availbale:10,
|
|
isAvaile:false
|
|
})
|
|
);
|
|
|
|
return(db);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 2. Insert multiple rooms (insertMany method) in the rooms collection
|
|
// member2
|
|
async function addManyFunc(db) {
|
|
|
|
await (
|
|
db.hotel.insertMany({
|
|
name:"double",
|
|
accomodates:3,
|
|
price:2000,
|
|
description:"Aroom fit for small family going on vacation",
|
|
rooms_availbale:5,
|
|
isAvaile:false
|
|
})
|
|
);
|
|
|
|
return(db);
|
|
};
|
|
|
|
// 3. Use the findOne method to search for a room with the name double.
|
|
async function findRoom(db) {
|
|
return await (
|
|
|
|
db.hotel.findOne({name:"double"})
|
|
|
|
);
|
|
};
|
|
|
|
// 4. Use the updateOne method to update the queen room and set the available rooms to 0.
|
|
|
|
function updateOneFunc(db) {
|
|
|
|
db.hotel.updateOne({name:"queen"}){
|
|
rooms_availbale:0
|
|
}
|
|
|
|
};
|
|
|
|
// 4.5
|
|
|
|
function updateOneFunc(db) {
|
|
await(
|
|
db.hotel.insertMany({
|
|
$set:{
|
|
name:"queen",
|
|
accomodates:4,
|
|
price:4000,
|
|
description:"A room with queen sixed bed for a simple gateaway",
|
|
rooms_availbale:15,
|
|
isAvaile:false
|
|
}
|
|
});
|
|
);
|
|
|
|
return(db);
|
|
};
|
|
|
|
|
|
// 5. Use the deleteMany method to delete all rooms that have 0 rooms available.
|
|
function deleteManyFunc(db) {
|
|
|
|
db.hotel.deleteMany({rooms_availbale:0})
|
|
|
|
};
|
|
|
|
|
|
|
|
try{
|
|
module.exports = {
|
|
addOneFunc,
|
|
addManyFunc,
|
|
updateOneFunc,
|
|
deleteManyFunc,
|
|
findRoom
|
|
};
|
|
} catch(err){
|
|
|
|
};
|
|
|