Skip to content

Instantly share code, notes, and snippets.

@flytzen
Created September 29, 2016 19:56
Show Gist options
  • Save flytzen/b8dbcb2079a66b1c8a67005ef5198052 to your computer and use it in GitHub Desktop.
Save flytzen/b8dbcb2079a66b1c8a67005ef5198052 to your computer and use it in GitHub Desktop.
namespace MissingProperty
{
using System;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json;
class Program
{
const string url = "https://[YourDocDbAccoubtName].documents.azure.com:443/";
const string key = "[YourKey]";
const string DbName = "MissingPropertyTest";
const string CollectionName = "Default";
static void Main(string[] args)
{
// This will cause DocumentDb to not store "null" values in the database as an example
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
var client = GetClient();
InitializeDb(client);
var item1 = new MyItem() {Id = "1", SomeValue = null };
var item2 = new MyItem() { Id = "2", SomeValue = "abc" };
var item3 = new MyItem() { Id = "3" };
var collectionUrl = UriFactory.CreateDocumentCollectionUri(DbName, CollectionName);
client.CreateDocumentAsync(collectionUrl, item1).Wait();
client.CreateDocumentAsync(collectionUrl, item2).Wait();
client.CreateDocumentAsync(collectionUrl, item3).Wait();
Console.WriteLine("Naive search");
var query1 = client.CreateDocumentQuery<MyItem>(collectionUrl).Where(i => i.SomeValue == null);
Console.WriteLine(query1.ToString());
foreach (var myItem in query1)
{
Console.WriteLine(myItem);
}
Console.WriteLine("Check for undefined property");
var query2 = client.CreateDocumentQuery<MyItem>(collectionUrl).Where(i => (i.SomeValue ?? null) == null);
Console.WriteLine(query2.ToString());
foreach (var myItem in query2)
{
Console.WriteLine(myItem);
}
Console.WriteLine("Done");
Console.ReadKey();
}
private static DocumentClient GetClient()
{
return new DocumentClient(new Uri(url), GetAuthKey(), new ConnectionPolicy() {ConnectionMode = ConnectionMode.Direct});
}
private static void InitializeDb(DocumentClient client)
{
CreateDatabaseIfNotExists(client).Wait();
CreateCollectionIfNotExists(client).Wait();
}
private static async Task CreateDatabaseIfNotExists(DocumentClient client)
{
if (!client.CreateDatabaseQuery()
.Where(db1 => db1.Id == DbName)
.ToList().Any())
{
await client.CreateDatabaseAsync(new Database { Id = DbName }).ConfigureAwait(false);
}
}
private static async Task CreateCollectionIfNotExists(DocumentClient docClient)
{
if (!docClient.CreateDocumentCollectionQuery(UriFactory.CreateDatabaseUri(DbName)).Where(c => c.Id == CollectionName).ToList().Any())
{
var docCollection = new DocumentCollection()
{
Id = CollectionName,
};
await docClient.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri(DbName), docCollection).ConfigureAwait(false);
}
}
private static SecureString GetAuthKey()
{
var results = new SecureString();
foreach (var ch in key)
{
results.AppendChar(ch);
}
return results;
}
}
public class MyItem
{
[JsonProperty("id")]
public string Id { get; set; }
public string SomeValue { get; set; }
public override string ToString()
{
return $"{this.Id} - {this.SomeValue ?? "NULL"}";
}
}
}
@flytzen
Copy link
Author

flytzen commented Sep 29, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment