Reputation: 135
guys, I use official CSharp Driver of mongodb, I got these two classes:
public class A
{
public A()
{
Bs = new List<B>();
}
[BsonId]
public string Id { get; set; }
public string Name { get; set; }
public IList<B> Bs { get; set; }
}
public class B
{
public string Name { get; set; }
}
in db, B is embedd in A document, entity is an object of A, then do the saving job like this:
collection.Save<A>(entity);
the Save method will perform either insert or update depends on the Id, my question is: when i do collection.Save(entity) the second time, that means entity document already exists in the A collection, does every fields in A and B document got updated? I dont know how the driver handler this kind of thing. supposed there ara a lot of B objects, let say 100 of them and not modified. what process going on?
Upvotes: 0
Views: 121
Reputation: 3568
When you call Save
, base on your [BsonId]
field driver decides what to do with this document:
Id
does not exist in collection, it will insert
this documentId
exists in collection, it will update
this document (based on your Id
). Update means it will replace all fields with the new fields from your entity
Here is an example how save
method works in pseudocode (the same as in C# driver):
function save( doc ) {
if( doc["_id"] ) {
update( {_id: doc["_id"] }, doc, { upsert: true } );
}
else {
insert(doc);
}
}
Upvotes: 1