0% found this document useful (0 votes)
22 views7 pages

QUIZ

The document describes updating and removing documents from a scores collection. It provides examples of using the update and remove methods in MongoDB to increment scores less than 70 by 20 and remove scores less than 60.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

QUIZ

The document describes updating and removing documents from a scores collection. It provides examples of using the update and remove methods in MongoDB to increment scores less than 70 by 20 and remove scores less than 60.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

QUIZ: MULTI-UPDATE

Recall the schema of the scores collection:


{
"_id" : ObjectId("50844162cb4cf4564b4694f8"),
"student" : 0,
"type" : "exam",
"score" : 75
}

Give every document with a score less than 70 an extra 20 points.

If you input an incorrect query, don't forget to reset the problem state, as any wrong update will
likely take you away from your initial state.

This is a fully functional web shell, so please press enter for your query to get passed to the
server, just like you would for the command line shell.

db.scores.update({score : {"$lt" : 70}}, {$inc: {score:20}}, {multi :true})


QUIZ: REMOVING DATA

Recall the schema of the scores collection:


{
"_id" : ObjectId("50844162cb4cf4564b4694f8"),
"student" : 0,
"type" : "exam",
"score" : 75
}

Delete every document with a score of less than 60.

This is a fully functional web shell, so please press enter for your query to get passed to the
server, just like you would for the command line shell.

db.scores.remove({score : {"$lt" : 60}})

need to know what type of documents restored in people and can map them to a strongly typed schema
in your objects

And this is generally preferable and right way to work with mongo db and we can create a class called
person that represents that.

QUIZ: .NET DRIVER, MONGOCLIENT,


MONGODATABASE, MONGOCOLLECTION
Which assembly contains the MongoClient class?

MongoDB.Driver

QUIZ: .NET DRIVER, DOCUMENT REPRESENTATION


What is the proper way to add an element to a BsonDocument? Check all that apply.

doc.Add(“field”, “value”);
doc[“field”] = “value”;
doc.Insert(“field”, “value”);
You can't add an element to a BsonDocument
QUIZ: .NET DRIVER, INSERTONE

Do you expect the second insert below to succeed?


var people = db.GetCollection<BsonDocument>(“people”);

var person = new BsonDocument


{
{ “name”, “Craig Wilson” },
{“company”, “MongoDB” }
};

await people.InsertOneAsync(person);
person.Remove(“_id”);
await people.InsertOneAsync(person);
Yes, because the Remove call will remove the _id field added by the driver in the first insert.

ONE: .NET DRIVER, FIND WITH FILTERS

Given the following class, what MongoDB filter is generated in the following Find method call?

class Person
{
[BsonElement(“name”)]
public string Name;

[BsonElement(“age”)]
public int Age;
}
var col = db.GetCollection<Person>("people");
var list = await col.Find(x => x.Name == "James" && x.Age <=
24).ToListAsync();
{ name: “James”, age: { $lte: 24 } }

QUIZ: .NET DRIVER, FIND WITH SKIP, LIMIT AND SORT

How many documents will be returned given the following Find method call?
col.Find(new BsonDocument())
.Limit(10)
.Skip(5)
.Limit(20);
20

QUIZ: .NET DRIVER, UPDATEONE AND UPDATEMANY

Given a collection with the following documents, how many will be affected by the below
update statement?
{ _id: 0, x: 1 }
{ _id: 1, x: 1 }
{ _id: 2, x: 1 }
{ _id: 3, x: 2 }
{ _id: 4, x: 2 }

await col.UpdateOneAsync(
filter: Query.EQ(“x”, 1),
update: Update.Set(“x”, 20));
QUIZ: .NET DRIVER, DELETEONE AND DELETEMANY

Given a collection with the following documents, how many will be affected by the below
update statement?
{ _id: 0, x: 1 }
{ _id: 1, x: 1 }
{ _id: 2, x: 1 }
{ _id: 3, x: 2 }
{ _id: 4, x: 2 }

await col.DeleteManyAsync(
filter: Query.EQ(“_id”, 1));
QUIZ: .NET DRIVER, BULKWRITE

Given a collection with the following documents, what will the collection look like after the
below BulkWriteAsync method is called? Check all that apply.
{ _id: 0, x: 1 }
{ _id: 1, x: 2 }
var models = new [] {
new UpdateManyModel<BsonDocument>(new BsonDocument(), Update.Set("x",
3)),
new UpdateManyModel<BsonDocument>(new BsonDocument(), Update.Inc("x",
1))
};

await col.BulkWriteAsync(models, new BulkWriteOptions { IsOrdered = false });

Week4

QUIZ: CREATING INDEXES

Please provide the mongo shell command to add an index to a collection named students, having
the index key be class, student_name.

Neither will go in the "-1" direction..

db.students.createIndex({class:1,student_name:1})

db.students.explain().find({‘scores’:$eleMatch: {type:’exam’,score:{‘$gt’:99.8}}}})

QUIZ: DOT NOTATION AND MULTIKEY

Suppose you have a collection called people in the database earth with documents of the
following form:
{
"_id" : ObjectId("551458821b87e1799edbebc4"),
"name" : "Eliot Horowitz",
"work_history" : [
{
"company" : "DoubleClick",
"position" : "Software Engineer"
},
{
"company" : "ShopWiki",
"position" : "Founder & CTO"
},
{
"company" : "MongoDB",
"position" : "Founder & CTO"
}
]
}

db.people.createIndex({'work_history.company' :-1})

QUIZ: INDEX CREATION OPTION, UNIQUE

Please provide the mongo shell command to create a unique index on student_id, class_id,
ascending for the collection students.
Ans : db.students.createIndex({student_id:1,class_id:1},{unique:true})
$elematch

You might also like