C#/.NET Driver Untyped API (using BSON...

3
C#/.NET Driver Untyped API (using BSON Documents) Initialization var connectionString = "mongodb://localhost/?safe=true"; var server = MongoServer.Create(connectionString); // creates a server var db = server.GetDatabase("hr"); // gets the hr database var collection = db.GetCollection("people"); // gets the people collection CRUD Operations Create var person = new BsonDocument { { "Name", "Jack" }, { "Age", 21 } }; collection.Save(person); // or collection.Insert(person); Read var query = Query.EQ("_id", person["_id"]); BsonDocument foundPerson = collection.FindOne(query); Update person["Name"] = "Jim"; collection.Save(person); // or var query = Query.EQ("_id", person["_id"]); var update = Update.Set("Name", "Jim"); collection.Update(query, update); collection.Remove(query); Delete var query = Query.EQ("_id", person["_id"]); collection.Remove(query); 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 MongoDB Reference Card

Transcript of C#/.NET Driver Untyped API (using BSON...

Page 1: C#/.NET Driver Untyped API (using BSON Documents)s3.amazonaws.com/info-mongodb-com/mongodb_qrc_CSharp_NET... · 2015-04-03 · Update Modifiers Update.Inc("a", 2) Increments aby

C#/.NET Driver Untyped API (using BSON Documents)

Initializationvar 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("people"); // gets the people collection

CRUD Operations

Createvar person = new BsonDocument{ { "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.EQ("_id", person["_id"]);var update = Update.Set("Name", "Jim");collection.Update(query, update);collection.Remove(query);

Deletevar query = Query.EQ("_id", person["_id"]);collection.Remove(query);

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

MongoDB Reference Card

Page 2: C#/.NET Driver Untyped API (using BSON Documents)s3.amazonaws.com/info-mongodb-com/mongodb_qrc_CSharp_NET... · 2015-04-03 · Update Modifiers Update.Inc("a", 2) Increments aby

Queries and What They Match

Query.EQ("a", 10) Docs where a is 10, or an array containing the value 10.

Query.And(Query.EQ("a", 10),Query.EQ("b", "hello"))

Docs where a is 10 and b is "hello".

Query.GT("a", 10) Docs where a is greater than 10. Also Query.LT (<), Query.GTE (>=), and Query.LTE (<=).

Query.In("a", new BsonArray { 10, "hello" }) Docs where a is either 10 or "hello".

Query.All("a", new BsonArray { 10, "hello"}) Docs where a is an array containing 10 and "hello".

Query.EQ("a.b", 10) Docs where a is an embedded document with b equal to 10.

Query.ElemMatch("a",Query.And(Query.EQ("b",1),Query.EQ("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.EQ("a", 1),Query.EQ("b", 2))

Docs where a is 1 or b is 2.

Query.Matches("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.NotIn("a", new BsonArray { 10, "hello" }) Docs where a is anything but 10 or "hello".

Query.Mod("a", 10, 1) Docs where a mod 10 is 1.

Query.Size("a", 3) Docs where a is an array with exactly 3 elements.

Query.Exists("a") Docs containing an a field.

Query.Type("a", BsonType.String) Docs where a is a string.

Query.Matches("a", "/foo.*bar/") Docs where a matches the regular expression foo.*bar.

Query.Not(Query.Type("a", BsonType.String)) Docs where a is not a string. Not negates any other query.

MongoDB Reference Cards // C#/.NET Driver: Untyped API

Page 3: C#/.NET Driver Untyped API (using BSON Documents)s3.amazonaws.com/info-mongodb-com/mongodb_qrc_CSharp_NET... · 2015-04-03 · Update Modifiers Update.Inc("a", 2) Increments aby

Update Modifiers

Update.Inc("a", 2) Increments a by 2.

Update.Set("a", 5) Sets a to the value of 5.

Update.Unset("a") Deletes the a key.

Update.Push("a", 1) Append the value 1 to the array a.

Update.PushAll("a", new BsonArray { 1, 2 }) Append both the values 1 and 2 to the array a.

Update.AddToSet("a", 1) Append the value 1 to the array a (if it isn’t already present).

Update.AddToSetEach("a", new BsonArray { 1, 2 })

Append both the values 1 and 2 to the array a (if they aren’t already present).

Update.PopFirst("a") Remove the first element from the array a.

Update.PopLast("a") Remove the last element from the array a.

Update.Pull("a", 5) Remove all occurences of 5 from the array a.

Update.PullAll("a", new BsonArray { 5, 6 }) Remove all occurences of 5 or 6 from the array a.

MongoDB Reference Cards // C#/.NET Driver: Untyped API

Created and distributed by MongoDB, Inc. For more information or to download MongoDB, visit mongodb.org or mongodb.com.