简体   繁体   English

选择不同的 mongodb C#

[英]select distinct mongodb C#

I have to select distinct records from my simple mongo db database.我必须从我的简单 mongo db 数据库中选择不同的记录。 I have many simple records these records looks like this :我有很多简单的记录,这些记录看起来像这样:

{"word":"some text"}

My code is very simple.我的代码很简单。

    const string connectionString = "mongodb://localhost";
    var client = new MongoClient(connectionString);

    MongoServer server = client.GetServer();
    MongoDatabase database = server.GetDatabase("text8");
    MongoCollection<Element> collection = database.GetCollection<Element>("text8");
    MongoCursor<Element> words = (MongoCursor<Element>)collection.FindAll();

But I have't idea how to select distinct word's from database.但我不知道如何从数据库中选择不同的单词。 Could someone can give me some advice ?有人可以给我一些建议吗?

MongoDB API has a distinct aggregation command, which returns distinct values found for a specified key in a collection. MongoDB API 有一个distinct聚合命令,它返回为集合中的指定键找到的不同值。 You can also use it from C# Driver :您也可以从C# Driver使用它:

var distinctWords = collection.Distinct("word");

where collection - is an instance from your example.其中collection - 是您示例中的一个实例。 This query will return all distinct values of word field in the collection.此查询将返回集合中word字段的所有不同值。

Also, as @JohnnyHK mentioned in comment, you can use linq approach , since it is supported by C# driver:此外,正如@JohnnyHK 在评论中提到的,您可以使用linq 方法,因为它受 C# 驱动程序支持:

var distinctWords = collection.AsQueryable<Element>().Select(e => e.Word).Distinct();

这项工作适合我

Collection.Distinct<string>("ColumnNameForDistinct", FilterDefinition<T>.Empty).ToListAsync()

My guess would be to make "word" an index on this db.我的猜测是让“word”成为这个数据库的索引。 Then using some linq to query it in a simple expression: var res = col.Query().Select(e => e.word).Distinct();然后使用一些 linq 在一个简单的表达式中查询它: var res = col.Query().Select(e => e.word).Distinct();

This would result in reading all words from the index.这将导致从索引中读取所有单词。

The MongoCollection.Distinct Method (String) V2.0 is Legacy MongoCollection.Distinct方法(字符串)V2.0遗留的

for new version API like 2.4 use:对于像 2.4 这样的新版本 API,请使用:

FieldDefinition<yueyun.land,string> field = "FirstName";
var bx = _yueyunlands.Distinct<string>(field, Builders<yueyun.land>.Filter.Empty).ToList();

If you want to filter first and get distinct afterwards and also do all of these at MongoDB side, you can use the following example.如果你想先过滤然后得到不同的,并在 MongoDB 端完成所有这些,你可以使用以下示例。 In this example I applied a filter, got distinct values and finally got count:在这个例子中,我应用了一个过滤器,得到了不同的值,最后得到了计数:

        var filter = Builders<Logs>.Filter.Ne(x => x.Id, null);
        var count = collection.Distinct(x => x.Id, filter).ToList().Count();

MongoDB doesn't have a built in operator to split a string of words from a query as there's not a way to split a string, then run a "distinct" operation on it. MongoDB 没有内置运算符来从查询中拆分一串单词,因为没有办法拆分字符串,然后对其运行“不同”操作。

One option would be to create a MapReduce and do the split in the MapReduce code and count each word.一种选择是创建一个MapReduce并在 MapReduce 代码中进行拆分并计算每个单词。 You can't do this with just C# code.仅使用 C# 代码无法做到这一点。

A second, and possibly simpler option would be to pre-split the field into words so that you could use one of the distinct operators:第二个可能更简单的选择是将字段预先拆分为单词,以便您可以使用不同的运算符之一:

{ "word": [ "some", "text"] }

Then:然后:

dbCollection.Distinct("word");

This would of course work if you just want to treat the entire string as a "word" rather than each word separately .如果您只想将整个字符串视为一个“单词”而不是将每个单词单独处理,这当然会起作用。 MapReduce's aren't real-time ... the pseudo-code would be: MapReduce 不是实时的……伪代码是:

map = function() {
  var splits = this.word.split(' ');
  for(var i = 0, l = splits.length; i < l; i++) {
     emit(splits[i], 1);
  }
}

reduce = function(word, vals) {
   var count = 0;
   for(var i=0, l=vals.length; i < l; i++) {
       count += vals[i];
   }
   return count;
}

When you run the MapReduce, it would be a collection of the number of occurrences of each word.当您运行 MapReduce 时,它​​将是每个单词出现次数的集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM