QUIZ
QUIZ
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.
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.
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.
MongoDB.Driver
doc.Add(“field”, “value”);
doc[“field”] = “value”;
doc.Insert(“field”, “value”);
You can't add an element to a BsonDocument
QUIZ: .NET DRIVER, INSERTONE
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.
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 } }
How many documents will be returned given the following Find method call?
col.Find(new BsonDocument())
.Limit(10)
.Skip(5)
.Limit(20);
20
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))
};
Week4
Please provide the mongo shell command to add an index to a collection named students, having
the index key be class, student_name.
db.students.createIndex({class:1,student_name:1})
db.students.explain().find({‘scores’:$eleMatch: {type:’exam’,score:{‘$gt’:99.8}}}})
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})
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