Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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"}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment