Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- db.User.insert([
- {
- _id: 100,
- fname: "John",
- surname: "Smith",
- age: 35,
- email: "jsmith@gmail.com",
- sex: "M",
- title:"Mr",
- car: {
- carReg: "131-G-101",
- fuel: "petrol"
- }
- },
- {
- _id: 101,
- fname: "Sean",
- surname: "Murphy",
- age: 22,
- email: "seanmurphy@yahoo.com",
- sex: "M",
- title:"Mr",
- car: {
- carReg: "172-G-200",
- electric: "true",
- }
- },
- {
- _id: 102,
- fname: "Aine",
- surname: "Browne",
- age: 23,
- email: "abrowne@gmail.com",
- sex: "F",
- title:"Ms"
- },
- {
- _id: 103,
- fname: "Alan",
- surname: "Murphy",
- age: 25,
- email: "murpha@hotmail.com",
- sex: "M"
- },
- {
- _id: 104,
- fname: "Sarah",
- surname: "Doyle",
- age: 24,
- email: "sarah@gmail.com",
- sex: "F",
- car: {
- carReg: "141-MO-123",
- fuel: "diesel",
- }
- },
- {
- _id: 105,
- fname: "Bill",
- surname: "Mulligan",
- age: 20,
- email: "billy123@gmail.com",
- sex: "M",
- car: {
- carReg: "12-RN-445",
- fuel: "petrol",
- }
- },
- {
- _id: 106,
- fname: "Shane",
- surname: "Kelly",
- age: 25,
- email: "sk998@yahoo.com",
- sex: "M",
- title : "Mr",
- car: {
- carReg: "11-WH-7783",
- fuel: "petrol",
- }
- },
- {
- _id: 107,
- fname: "Will",
- surname: "Doyle",
- age: 20,
- email: "doyler123@gmail.com",
- sex: "M",
- title : "Mr"
- }
- ]);
- //age 19
- db.User.find({age: 19})
- //greater than 19
- db.User.find({age: {$gt: 19}})
- //greater than 19 & has car
- db.User.find({age: {$gt: 19}, carReg: {$exists:true}})
- //greater than 1105 & age > 20
- db.User.find({_id: {$gt: 104}, age: {$gt: 20}})
- //greater than 19 & has car
- db.User.findOne({ carReg: {$exists:true}})
- /*9. User 106 - Shane has bought a car with reg 12-G-1234. The following command was run to update the user’s document:
- db.users.save({_id:106, carReg:"12-G-1234"})
- What does the document look like now and why?
- */ -->Bill now has a car reg added to obj
- /*10. User 102 - Sean has bought a car with reg 10-G-9876. The following command was run to update the user’s document:
- db.user.update({_id:102}, {carReg:"10-G-9876"})
- What does the document look like now and why?
- *? */--> over writtes record to contain only id & carReg
- /*11. User 105 – Bill’s document is as follows:
- { "_id" : 105, "fname" : "Bill", "surname" : "Mulligan", "age" : 19, "email" : "billy123@gmail.com" }
- Bill has bought a car with reg 161-MO-4. Give the command so that Bill’s document now looks as follows:
- db.Users.update{ "_id" : 105, "fname" : "Bill", "surname" : "Mulligan", "age" : 19, "email" : "billy123@gmail.com", "carReg" : "161-MO-4" }
- */
- db.User.update({_id: 106},{carReg : "161-MO-4" })
- /*12. User 106’s document now looks as follows:
- { "_id" : 106, "carReg" : "12-G-1234" }
- */
- db.User.save({
- _id: 107,
- fname: "Shane",
- surname: "Kelly",
- age: 24,
- email: "sk998@yahoo.com",
- carReg: "12-G-1234"
- })
- //13. Give the mongodb command to add 1 to each user’s age.
- db.User.update({age: {$exists:true}},{$inc:{age:1}},{multi:true})
- db.User.update([
- {{ _id: 100},{$set :{sex:"M"}}},
- {{ _id: 101},{$set :{sex:"M"}}},
- {{ _id: 102},{$set :{sex:"M"}}},
- {{ _id: 103},{$set :{sex:"M"}}},
- {{ _id: 104},{$set :{sex:"F"}}},
- {{ _id: 105},{$set :{sex:"M"}}},
- {{ _id: 106},{$set :{sex:"M"}}},
- {{ _id: 107},{$set :{sex:"M"}}}
- ])
- db.User.updateMany([
- { _id: 101},{$set :{sex:"M"}},
- { _id: 102},{$set :{sex:"M"}},
- { _id: 103},{$set :{sex:"M"}},
- { _id: 104},{$set :{sex:"F"}},
- { _id: 105},{$set :{sex:"M"}},
- { _id: 106},{$set :{sex:"M"}},
- { _id: 107},{$set :{sex:"M"}}
- ])
- //16.Users 101 – Sean, 103 – Alan and 107 – Will have sold their cars, update the
- db.User.update({_id: 101},{$unset:{carReg:0}})
- db.User.update({_id: 107},{$unset:{carReg:0}})
- //17.Give the mongodb command to list/show only the fname, surname,
- // age and sex attributes of documents where the _id is between 101 and 107 inclusive.
- db.User.find({_id: {$gt: 104}, age: {$gt: 20}})
- ///lab2:
- //show indices
- db.User.getIndexes()
- //create index on age
- db.User.createIndex({age: 1})
- //4.Give the mongodb
- // command to create a descending index on the surname attribute
- db.User.createIndex({surname: -1})
- //5. Give the mongodb command to create
- // a descending index on the age attribute.
- db.User.createIndex({age: -1})
- //6. Give the mongodb command to remove
- // the ascending index on the age attribute.
- db.User.dropIndex({age: 1})
- //7. Give the mongodb command to show only the fname and
- //surname attributes of users who don’t have a car.
- db.User.find({car: {$exists:false}},{_id: 0,fname: 1,surname:1})
- // 8.Give the mongodb command to show only the fname,
- // surname, reg and fuel attributes of users who have a car.
- db.User.find({car: {$exists:true}},
- {_id: 0, fname: 1, surname:1, "car.fuel" : 1})
- //9.Give the mongodb command to list details of all users
- // (except for their car). The list should be in sorted by name.
- db.User.find({_id: {$exists:true}},{"car": 0}).sort({fname: 1})
- //10.Give the mongodb command to list only details of all cars.
- // The list should be in sorted by car registration.
- db.User.find({car: {$exists:true}}, {_id: 0,fname: 0,surname:0, age:0, email: 0, sex: 0, title:0}).sort({"car.carReg": 1})
- //11.Give the mongodb command to list the documents that have an
- //electric attribute in the car attribute.
- db.User.find({"car.electric": {$exists:true}})
- //12.Give the mongodb command to list only the fname and surname
- // attributes of users who have a petrol car.
- db.User.find({"car.fuel":"petrol"}, {_id: 0,fname: 1,surname:2})
- //14. Give the mongodb command to delete all females between the ages
- //of 21 and 24 inclusive.
- db.User.deleteMany({age:{$gt:20, $lt: 25}})
- //15.Give the mongodb command to delete the fuel attribute from all documents.
- //db.User.deleteMany({car.fuel:{"petrol"})
- db.User.update({car: {$exists:true}},
- {$unset: {"car.fuel":1}},
- {multi: true})
- //16. Give the mongodb command to give everyone who owns a car the title of Owner.
- db.User.update({car: {$exists:true}},
- {$set: {title: "Owner"}},
- {multi: true})
- //17.Give the mongodb command to increase the age of all Males who own a car by 10 years.
- db.User.update({car: {$exists:true}},
- {$inc: {age: 10}},
- {multi: true})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement