以下是我的Mongodb数据结构。我想在c#中查询,我可以根据customerid,protofolio和元标记搜索项目。
{
"_id" : ObjectId("512aed363ca67cd8a6f2f3e2"),
"ReferenceNumber" : "MongoDb",
"Title" : "Shashi",
"CreatedUserId" : "users/1027",
"Content" : "s",
"CustomerId" : "customers/257",
"StatusDefinitionId" : "sd",
"ItemMetadata" : [{
"MetadataId" : "MetadataDefinitions/839",
"MetadataName" : "DropDown",
"MetadataValue" : "DropDown1"
}, {
"MetadataId" : "MetadataDefinitions/838",
"MetadataName" : "Number1",
"MetadataValue" : "123"
}],
"Portfolios" : [{
"_id" : "portfolios/225",
"IsDefault" : true
}]
}
我正在使用以下查询来搜索数据,具体取决于客户,protofolio和metadatags,我正在传递此信息。搜索(“123&”,“portfolio / 225”,null,null,null,null,item );和item有customerid
public ItemDetails Search(string metaDataTags, string portfolios, string statusDefinitions, int? numberOfItemsRequired, int? limit, int? offset, Item item)
{
ItemDetails searchedItemDetails = null;
// Extract the tags from the metaDataTags into an array.
var tags = new string[0];
var portfolioIds = new string[0];
var statusDefinitionsIds = new string[0];
// The metatag search data sample format: "subject=physics&grade=tough&marks=150"
if (!string.IsNullOrEmpty(metaDataTags))
{
// Split the string into tags array based on & separator.
tags = metaDataTags.ToLower().Split('&');
}
if (!string.IsNullOrEmpty(portfolios))
{
// Split the string into tags array based on , separator.
portfolioIds = portfolios.ToLower().Split(',');
}
if (!string.IsNullOrEmpty(statusDefinitions))
{
// Split the string into tags array based on & separator.
statusDefinitionsIds = statusDefinitions.Split(',');
}
tags = tags.Where(x => !string.IsNullOrEmpty(x))
.Select(x => Regex.Replace(Regex.Replace(x, @"\s+$", string.Empty), @"^\s+", string.Empty))
.ToArray();
var portFolioData = new BsonArray(portfolioIds);
var Metatags = new BsonArray(tags);
var query = Query.And(Query.EQ("CustomerId", item.CustomerId), Query.In("Portfolios._id", portFolioData), Query.In("ItemMetadata.MetadataValue", Metatags));
var itemsForCustommer = _items.Find(query);
// searchedItemDetails.Items = itemsForCustommer.ToList();
return searchedItemDetails;
}
}