下面是我的MongoDB数据结构。我想要一个 C# 中的查询,我可以根据 customerid、protofolio 和 metatags 搜索项目。
{
"_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 搜索数据,并且我正在传递 this.Search("123&", "portfolios/225", null, null, null, null, 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;
}
}