[英]Trying to compare tags in mongodb string array, with textbox input c#
好的,我有一個mongodb,它具有一個稱為視頻的集合,在視頻中,我有一個稱為標簽的字段。 我想做的是將文本框輸入與集合中所有視頻上的標簽進行比較,如果標簽與文本框的輸入匹配,則將它們返回到gridview。 當我創建新視頻時,標簽字段是一個字符串數組,因此可以存儲多個標簽。 我正在嘗試在C#中執行此操作。 希望你們中的一些可以幫助謝謝!
用於創建新視頻文檔的代碼。
#region Database Connection
var client = new MongoClient();
var server = client.GetServer();
var db = server.GetDatabase("Database");
#endregion
var videos = db.GetCollection<Video>("Videos");
var name = txtVideoName.Text;
var location = txtVideoLocation.Text;
var description = txtVideoDescription.Text;
var user = txtVideoUserName.Text;
string[] lst = txtVideoTags.Text.Split(new char[] { ',' });
var index = videos.Count();
var id = 0;
if (id <= index)
{
id += (int)index;
}
videos.CreateIndex(IndexKeys.Ascending("Tags"), IndexOptions.SetUnique(false));
var newVideo = new Video(id, name, location, description, lst, user);
videos.Insert(newVideo);
好吧,這就是搜索方法的樣子,就像我剛剛使語法與Grant Winney所講的稍有不同。
var videos = db.GetCollection<Video>("Videos");
string[] txtInput = txtSearchTags.Text.Split(new char[] { ',' });
var query = (from x in videos.AsQueryable<Video>()
where x.Tags.ContainsAny(txtInput)
select x);
假設MongoDB驅動程序可以將所有視頻的標簽包含在TextBox
指定的標簽的所有視頻中查找,則可以將其正確轉換為有效查詢。
var videos = db.GetCollection<Video>("Videos")
.AsQueryable()
.Where(v => v.Tags.Split(',')
.ContainsAny(txtVideoTags.Text.Split(',')))
.ToList();
確保您已using MongoDB.Driver.Linq;
在頂部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.