Lab6 1
Lab6 1
I. Objectives
Overview:
find(query, projection)
findOne(query, projection)
update(query, update)
remove(query, justOne)
❖ Create
Or
The difference with save is that if the passed document contains an _id field, if a document already
exists with that _id it will be updated instead of being added as new.
db.people.insertMany([{name: 'Tom', age: 28},{name: 'John', age: 25}, {name: 'Kathy', age: 23}])
❖ Update
db.people.updateOne({name: 'Tom'},{age: 29, name: 'Tom'}) //Will replace only first matching
document.
documents.
You can also update multiple documents simultaneously by adding a third parameter. This query
If a new field is coming for update, that field will be added to the document.
can be used.
Note: Fields you use to identify the object will be saved in the updated document. Field that are
not defined in the update section will be removed from the document.
❖ Delete
db.people.deleteMany({name: 'Tom'})
Or just one
db.people.deleteOne({name: 'Tom'})
MongoDB's remove() method. If you execute this command without any argument or without empty
db.people.remove();
or
db.people.remove({});
❖ Read
Query for all the docs in the people collection that have a name field with a value of 'Tom':
db.people.find({name: 'Tom'})
db.people.findOne({name: 'Tom'})
You can also specify which fields to return by passing a field selection parameter. The following
will exclude the _id field and only include the age field:
Note: by default, the _id field will be returned, even if you don't ask for it. If you would like not to
get the _id back, you can just follow the previous example and ask for the _id to be excluded by
If you want to find sub record like address object contains country, city, etc.
db.people.find({'address.country': 'US'})
Remember that the result has a `.pretty()` method that pretty-prints resulting JSON:
db.people.find().pretty()
You can use other operators besides $set when updating a document.
The $push operator allows you to push a value into an array, in this case we will add a new nickname to
the nicknames array.
// This adds the string 'Tommy' into the nicknames array in Tom's document.
The $pull operator is the opposite of $push, you can pull specific items from arrays.
// This removes the string 'Tommy' from the nicknames array in Tom's document.
The $pop operator allows you to remove the first or the last value from an array.
Let's say Tom's document has a property called siblings that has the value ['Marie', 'Bob', 'Kevin', 'Alex'].
// This will remove the first value from the siblings array, which is 'Marie' in this case.
// This will remove the last value from the siblings array, which is 'Alex' in this case.
db.collection.update(
query,
update,
{
upsert: boolean,
multi: boolean,
writeConcern: document
}
)
➔ multi is optional. If set to true, updates multiple documents that meet the query criteria.
If set to false, updates one document. The default value is false.
Update Tom's marks to 55 where marks are 50 (Use the positional operator $):
- For the following schema: {name: 'Tom', age: 28, marks: [{subject: "English", marks:
90},{subject: "Maths", marks: 100}, {subject: "Computes", marks: 20}]}
// By using {name: "Tom", "marks.subject": "English"} you will get the position of the object in the
marks array, where subject is English. In "marks.$.marks", $ is used to update in that position of
The positional $ operator identifies an element in an array to update without explicitly specifying
To update 80 to 82 in the grades array in the first document, use the positional $ operator if you do
db.students.update(
{ _id: 1, grades: 80 },
{ $set: { "grades.$" : 82 } })
{
"address": {
"building": "1007",
"coord": [-73.856077, 40.848447],
"street": "Morris Park Ave",
"zipcode": "10462"
},
"cuisine": "Bakery",
"grades": [ {
"grade": "A",
"score": 2
- Check the insertion of the document using the function count () to count the number of documents
in a collection. (db.collectionName.count()).
- Insert documents described in the file “Lab_Restaurants” in the collection “Restaurant”.
- Propose the appropriate queries to respond to following requests:
1. Show restaurants located in the Brooklyn area (borough= “Brooklyn”).
2. Show only the names and addresses of these restaurants.
3. Determine the names of restaurants with the cuisine type "Bakery" and located in "Brooklyn" area.
4. Determine the names of restaurants that have the cuisine type "Bakery" or are in the "Brooklyn" area.
5. Determine the names of the restaurants having as type of cuisine "Ice Cream", "Yogurt" or
"Hamburgers".
6. Determine the names of the restaurants that necessarily have "Ice Cream" and "Yogurt" as their type of
cuisine.
7. Determine the names of restaurants in the "Brooklyn" area that do not have "Hamburgers" as their type
of cuisine.
8. Determine the names of restaurants that do not have any grades.
9. Determine the names of the restaurants that did not obtain grades of type "B" and "Z".
10. Determine the names of the restaurants having obtained grades between 10 and 14.
11. Determine the names of restaurants with "B" grades and grades > 10.
12. For all the restaurant located in the Brooklyn area and having “A” as grade, change their score to 5.
13. Delete all the restaurant having a score >10.