我正在关注Microsoft的这篇博客文章,测试DocumentDB。
我已经创建了一个集合,并通过我的应用程序中的不同POCO类插入了2个文档。它创建了文档,但是我无法将它们过滤回各自的POCO类中。我意识到我正在查询所有集合,因此显然是在检索存储在该集合中的所有文档。
在查询时区分文档的最佳方法是什么,以便我可以按类型分别查询它们?
我可以在类型字段添加到文档中,可以通过获取WHERE type="user"
,但我不知道,我不能做SELECT * FROM users
与users
作为一个文档类型(如果在DocumentDB这样的事情),而不是一个集合。
这是我创建文档的方式:
var user1= new User()
{
UserTypeId = 0,
UserName = "[email protected]",
Password = "12345",
PasswordSalt = "saltyPassword",
UserStatusId = 1,
ProfilePhotoKey = "KJSY"
};
await DocumentDBRepository<User>.CreateItemAsync(user1);
var client = new Client()
{
ClientName = "client1",
Secret = "rxPBsIVYya2Jg2ZHPNG8gL0P36TnutiBehvEFgk938M=",
Title = "Administration Front End Application",
ApplicationTypeId = 0,
Active = false,
RefreshTokenLifeTime = 60,
AllowedOrigin = "http://localhost:8080",
AllowedRoles = "admin"
};
await DocumentDBRepository<Client>.CreateItemAsync(client);
文档Db存储库类
public static class DocumentDBRepository<T>
{
//Use the Database if it exists, if not create a new Database
private static Database ReadOrCreateDatabase()
{
var db = Client.CreateDatabaseQuery()
.Where(d => d.Id == DatabaseId)
.AsEnumerable()
.FirstOrDefault();
if (db == null)
{
db = Client.CreateDatabaseAsync(new Database { Id = DatabaseId }).Result;
}
return db;
}
//Use the DocumentCollection if it exists, if not create a new Collection
private static DocumentCollection ReadOrCreateCollection(string databaseLink)
{
var col = Client.CreateDocumentCollectionQuery(databaseLink)
.Where(c => c.Id == CollectionId)
.AsEnumerable()
.FirstOrDefault();
if (col == null)
{
var collectionSpec = new DocumentCollection { Id = CollectionId };
var requestOptions = new RequestOptions { OfferType = "S1" };
col = Client.CreateDocumentCollectionAsync(databaseLink, collectionSpec, requestOptions).Result;
}
return col;
}
//Expose the "database" value from configuration as a property for internal use
private static string databaseId;
private static String DatabaseId
{
get
{
if (string.IsNullOrEmpty(databaseId))
{
databaseId = ConfigurationManager.AppSettings["database"];
}
return databaseId;
}
}
//Expose the "collection" value from configuration as a property for internal use
private static string collectionId;
private static String CollectionId
{
get
{
if (string.IsNullOrEmpty(collectionId))
{
collectionId = ConfigurationManager.AppSettings["collection"];
}
return collectionId;
}
}
//Use the ReadOrCreateDatabase function to get a reference to the database.
private static Database database;
private static Database Database
{
get
{
if (database == null)
{
database = ReadOrCreateDatabase();
}
return database;
}
}
//Use the ReadOrCreateCollection function to get a reference to the collection.
private static DocumentCollection collection;
private static DocumentCollection Collection
{
get
{
if (collection == null)
{
collection = ReadOrCreateCollection(Database.SelfLink);
}
return collection;
}
}
//This property establishes a new connection to DocumentDB the first time it is used,
//and then reuses this instance for the duration of the application avoiding the
//overhead of instantiating a new instance of DocumentClient with each request
private static DocumentClient client;
private static DocumentClient Client
{
get
{
// change policy to ConnectionMode: Direct and ConnectionProtocol: TCP on publishing to AZURE
if (client == null)
{
string endpoint = ConfigurationManager.AppSettings["endpoint"];
string authKey = ConfigurationManager.AppSettings["authKey"];
Uri endpointUri = new Uri(endpoint);
client = new DocumentClient(endpointUri, authKey);
}
return client;
}
}
/* QUERY HELPERS */
public static IEnumerable<T> GetAllItems()
{
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink)
.AsEnumerable();
}
public static IEnumerable<T> GetItems(Expression<Func<T, bool>> predicate)
{
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink)
.Where(predicate)
.AsEnumerable();
}
public static async Task<Document> CreateItemAsync(T item)
{
return await Client.CreateDocumentAsync(Collection.SelfLink, item);
}
public static T GetItem(Expression<Func<T, bool>> predicate)
{
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink)
.Where(predicate)
.AsEnumerable()
.FirstOrDefault();
}
public static async Task<Document> UpdateItemAsync(string id, T item)
{
Document doc = GetDocument(id);
return await Client.ReplaceDocumentAsync(doc.SelfLink, item);
}
private static Document GetDocument(string id)
{
return Client.CreateDocumentQuery(Collection.DocumentsLink)
.Where(d => d.Id == id)
.AsEnumerable()
.FirstOrDefault();
}
}
我正在尝试获得:
var q = DocumentDBRepository<User>.GetAllItems().ToList();
var t = DocumentDBRepository<Client>.GetAllItems().ToList();
q应该只包含那些由...创建的用户文档
await DocumentDBRepository<User>.CreateItemAsync(user1);
并且t应该仅包含那些由...创建的客户文档
await DocumentDBRepository<Client>.CreateItemAsync(client1);
由于DocumentDB的type
每个文档都没有内置的元数据,因此type
在将异构文档存储在同一集合中时,您需要添加一个(例如,建议的属性或任何其他区分属性),并在您的文档中使用它WHERE
条款。您为该属性命名的内容以及在其中存储的值与集合名称无关。
关于您的特定示例,它SELECT * from users WHERE type='user'
可以工作,但是SELECT * from users
无论类型如何,都将返回所有文档。
默认情况下,所有属性都被索引,包括新形成的type
属性,这使您可以高效地执行WHERE子句过滤,而无需进行收集扫描。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句