Skip to content

Instantly share code, notes, and snippets.

@craiggwilson
Created June 9, 2015 17:34
Show Gist options
  • Save craiggwilson/f7145d6b00beaf9d0bdb to your computer and use it in GitHub Desktop.
Save craiggwilson/f7145d6b00beaf9d0bdb to your computer and use it in GitHub Desktop.
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.Serialization.Attributes;
class Entity
{
public ObjectId Id { get; set; }
public string Prop1 { get; set; }
public List<EmbeddedEntity> Embeds { get; set; }
public Entity()
{
Embeds = new List<EmbeddedEntity>();
}
}
class EmbeddedEntity
{
public string Prop2 { get; set; }
}
var client = new MongoClient();
var db = client.GetDatabase("test");
var col = db.GetCollection<Entity>("entities");
db.DropCollectionAsync("entities").Wait();
var entity = new Entity { Prop1 = "Yes!" };
col.InsertOneAsync(entity).Wait();
var update = Builders<Entity>.Update.Push(x => x.Embeds, new EmbeddedEntity { Prop2 = "No!!!" });
col.UpdateOneAsync(x => x.Id == entity.Id, update).Wait();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment