Transactions, Joins, Indexes, Fragments, oh my….
So this is where having SQL Server as your document database starts to pay off….you can actually join your document collections together…even to other tables that have nothing to do with BsonSqlServer.
Need to update your document data along with other data as part of a transaction, no problem.
Try to do that with MongoDB.
Simple Queries
How you query depends on how you want to deal with the data returned. You always have a choice - treat the data as a raw BsonDocument or have it deserialized into an object representation of your choice.
The awesome power of treating your data as documents means that in 1 collection - say “People” - you can have documents with completely different structures and content - athletes, doctors, students, etc. - yet query them all as “People”.
A query in BsonSqlServer is always made against a collection. First you create the Query object, and then execute it:
Query q = QueryBuilder.WhereStatic( "FirstName", Comparison.Equals, "Steve"); BsonSqlCursor<People> results = peopleCollection.FindAs<People>(q); for each(Person p in results) { }
Need it to be part of a transaction:
BsonSqlCursor<People> results = peopleCollection.FindAs<People>(q).SetTransaction(yourSqlTransaction);
You can also compile queries and bind them to object values at runtime, which improves performance if you are executing the same query over and over.
For example, to always find people with the same name as a person object sent to the server, you would create your compiled query in some static setup code somewhere:
Query nameSearch = QueryBuilder.WhereBound("FirstName", Comparison.Equals).Compile();
Then, in your code responding to some user input, your search would be executed by binding the query:
BsonSqlCursor<People> results = peopleCollection.FindAs<People>(nameSearch.Bind(personSentAsInput));
This would find the “FirstName” property in the object you bound it to and substitute it as the parameter value to the sql query.
Working with Cursors
When you execute a query, it always returns a BsonSqlCursor, very similar to the way the MongoDB C# Driver works. You can type the cursor, as we did in the examples above, or have the cursor return your data as native BsonDocuments. BsonDocuments give you the most flexibility in working with your document data, but require you to know how to access the document properties.
53 Notes/ Hide
- bsonsqlserver posted this
- Show more notes