C#/.NET Driver Typed API (using POCOs)
Initializationclass Person{ public ObjectId Id { get; set; } public string Name { get; set; } public int Age { get; set; }}
var connectionString = "mongodb://localhost/?safe=true";var server = MongoServer.Create(connectionString); // creates a server
var db = server.GetDatabase("hr"); // gets the hr databasevar collection = db.GetCollection<Person>("people"); // gets the people collection
CRUD OperationsCreatevar person = new Person{ Name = "Jack", Age = 21};
collection.Save(person);
// or
collection.Insert(person);
Readvar query = Query.EQ("_id", person["_id"]);
BsonDocument foundPerson = collection.FindOne(query);
Updateperson.Name = "Jim";collection.Save(person);
// or
var query = Query<Person>.EQ(p => p.Id, person.Id);var update = Update<Person>.Set(p => p.Name, "Jim");collection.Update(query, update
Deletevar query = Query<Person>.EQ(p => p.Id, person.Id);collection.Remove(query
MongoDB Reference Card
The C#/.NET driver is the client-side connector used to talk to MongoDB servers. It is available for download from github or nuget.
i
Queries and What They Match
Query<P>.EQ(p => p.A, 10) Docs where A is 10, or an array containing the value 10.
Query.And(Query<P>.EQ(p => p.A, 10)Query<P>.EQ(p => p.B, "hello"))
Docs where A is 10 and B is "hello".
Query<P>.GT(p => p.A, 10) Docs where A is greater than 10. Also Query.LT (<), Query.GTE (>=), and Query.LTE (<=).
Query<P>.In(p => p.A, new [] { 10, 42 }) Docs where A is either 10 or 42.
Query<P>.All(p => p.A, new [] { 10, 42}) Docs where A is an array containing 10 and 42.
Query<P>.EQ(p => p.A.B, 10) Docs where A is an embedded document with B equal to 10.
Query<P>.ElemMatch(p => p.A,qb => qb.And(qb.EQ(a => a.B, 1)qb.EQ(a => a.C, 2)))
Docs where A is an array containing at least one item with both B equal to 1 and C equal to 2 in the same item.
Query.Or(Query<P>.EQ(p => p.A, 1),Query<P>.EQ(p => p.B, 2))
Docs where A is 1 or B is 2.
Query<P>.Matches(p => p.A, "/̂ m/") Docs where A begins with the letter m.
The following queries cannot use indexes as of MongoDB v2.0. These query forms should normally be accompanied by at least one other query term which does use an index.
Query<P>.NotIn(p => p.A, new [] { 10, 42 }) Docs where A is anything but 10 or 42.
Query<P>.Mod(p => p.A, 10, 1) Docs where A mod 10 is 1.
Query<P>.Size(p => p.A, 3) Docs where A is an array with exactly 3 elements.
Query<P>.Exists(p => p.A) Docs containing an A field.
Query<P>.Type(p => p.A, BsonType.String) Docs where A is a string.
Query<P>.Matches(p => p.A, "/foo.*bar/") Docs where A matches the regular expression foo.*bar.
Query.Not(Query<P>.Type(p => p.A, BsonType.String))
Docs where A is not a string. Not negates any other query.
MongoDB Reference Cards // C#/.NET Driver: Typed API
Update Modifiers
Update<P>.Inc(p => p.A, 2) Increments A by 2.
Update<P>.Set(p => p.A, 5) Sets A to the value of 5.
Update<P>.Unset(p => p.A) Deletes the A key.
Update<P>.Push(p => p.A, 1) Append the value 1 to the array A.
Update<P>.PushAll(p => p.A, new [] { 1, 2 }) Append both the values 1 and 2 to the array A.
Update<P>.AddToSet(p => p.A, 1) Append the value 1 to the array A (if it isn’t already present)
Update<P>.AddToSetEach(p => p.A, new [] { 1, 2 })
Append both the values 1 and 2 to the array A (if they aren’t already present).
Update<P>.PopFirst(p => p.A) Remove the first element from the array A.
Update<P>.PopLast(p => p.A) Remove the last element from the array A.
Update<P>.Pull(p => p.A, 5) Remove all occurences of 5 from the array A.
Update<P>.PullAll(p => p.A, new [] { 5, 6 }) Remove all occurences of 5 or 6 from the array A.
Created and distributed by MongoDB, Inc. For more information or to download MongoDB, visit mongodb.org or mongodb.com.
MongoDB Reference Cards // C#/.NET Driver: Typed API