2016-08-25 15 views
1

MongoDBから検索結果を取得しようとしていますが、配列の配列をMogoDBドキュメントの特定のStringオブジェクトと照合しています。 私のサンプルMongoDbドキュメント。MongoDBの配列と文字列を一致させる

"Notedisp" : { 
    "NoteID" : NumberLong(100281), 
    "NoteTitle" : null, 
    "NoteContent" : "In mathematics, the Pythagorean theorem theorem, also known as Pythagoras's theorem, is a relation in Euclidean geometry among the three sides of a right triangle. It states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. The theorem can be written as an equation relating the lengths of the sides a, b and c, often called the \"Pythagorean equation\"\r\n\r\na^2 + b^2 = c^2 ,\r\nwhere c represents the length of the hypotenuse and a and b the lengths of the triangle's other two sides.",  
}, 

私はすでに次のコードを試しています。上記のコード「Notedisp.NoteContent」で

var listTearm = ["what","is","Pythagorean","theorem"] 
var filter = (builder.AnyIn("Notedisp.NoteContent", listTearm) 

はMongoDBのドキュメント内の文字列オブジェクトであり、それは、空の文字列を返します。それで、StringとMogoDbのドキュメントを一致させ、特定のデータを返すことができる特定の方法があります。単純化されたオブジェクトに

+0

これは正しい軌道に乗るでしょうか? https://docs.mongodb.com/manual/reference/operator/query/text/#match-any-of-the-search-terms – dyouberg

答えて

0

public class Note 
{ 
    [BsonId] public long Id { get; set; } 
    [BsonElement("content")] public string Content { get; set; } 
} 

は、まずあなたのコレクションにテキストインデックスを作成する必要があります。

IndexKeysDefinition<Note> keys = "{ content: \"text\" }"; 
    string contentTextSearchIndex = await _collection.Indexes.CreateOneAsync(keys); 

を、その後$text演算子で検索語のいずれかに一致

var fdb = Builders<Note>.Filter; 
var cursor = await _collection.FindAsync(fdb.Text("what is Pythagorean theorem")); 
var docs = await cursor.ToListAsync(); 
関連する問題