【问题标题】:Match Array to String in MongoDB在 MongoDB 中将数组与字符串匹配
【发布时间】:2016-08-25 12:05:24
【问题描述】:

我正在尝试通过将字符串数组与 MogoDB 文档中的特定字符串对象匹配来从 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.",       
 },

我已经尝试过以下代码。

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

在上面的代码中,“Notedisp.NoteContent”是 MongoDB 文档中的一个字符串对象,它返回的是空字符串。那么有没有什么特定的方法可以让我将 String 匹配到 MogoDb 文档并返回特定的数据。

【问题讨论】:

标签: c# mongodb search match


【解决方案1】:

用一个简化的对象,

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2021-04-28
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    相关资源
    最近更新 更多