0

The complete method should be generic like

public string strGetMaxValue(string strDBName, string strCollectionName, string strKey)
{
 // in this method if pass some prms it should give max value
}

The one i tried is

string strMaxValue = "";            
        MongoServer objServer = this.ConnectToServer();
        if ((strDBName != null || strDBName != "") && (strCollectionName != null || strCollectionName != ""))
        {
            string[] strArrays = new string[1];
            strArrays[0] = strKey;
            //MongoCursor<BsonDocument> objCursor = objServer.GetDatabase(strDBName).GetCollection(strCollectionName).Find(query).SetSortOrder(SortBy.Descending(strArrays)).SetLimit(1);

            var objCursor = objServer.GetDatabase(strDBName).GetCollection(strCollectionName).FindAll().SetSortOrder(SortBy.Descending(strArrays)).SetLimit(1).ToArray();


        }

In that objCursor i m getting that document which i need. i want to extract that field value and needs to send it as return parameter.

The method should be generic as such the key value may a field in nested document also.

how to achieve this.?

4

1 回答 1

0

The method you are looking for is SetFields(params string[] fields) - it can be called on a cursor. It will limit your result set to just the fields you pass in (array) as well as the id. You can then index the field using the []

        var result = server
            .GetDatabase(strDBName)
            .GetCollection(strCollectionName)
            .FindAll()
            .SetSortOrder(SortBy.Descending(new [] {strKey}))
            .SetFields(new [] {strKey}) // The way to wrap something in an array for reference
            .SetLimit(1)
            .FirstOrDefault(); // Will return null if there are no rows

        // There is a chance that we have no results
        if (result != null)
            // You might want to make sure this is a string / add the datatype
            // as a Generic T to your function
            return result[strKey].AsString;
        else
            return null;
于 2013-06-27T00:48:29.220 回答