Choose a great mongodb c #

I need to select individual records from my simple mongo db database. I have many simple entries, these entries look 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 do not know how to choose a great word from the database. Can someone give me some advice?

+4
source share
4 answers

The MongoDB API has a command distinctthat returns the various values ​​found for the specified key in the collection. You can also use it from C # Driver :

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

where collectionis an example from your example. This query will return all the different field values wordin the collection.

, @JohnnyHK, , linq, #:

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

"" db. , linq :   var res = col.Query(). (e = > e.word).Distinct();

.

+1

MongoDB , , "" .

MapReduce MapReduce . #.

, , - , distinct :

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

:

dbCollection.Distinct("word");

, , , "", , 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;
}

MapReduce, .

0

MongoCollection.Distinct (String) V2.0

API, 2.4, :

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

All Articles