开发者

MongoDB C# map an Item

i´m using map/reduce to find the max Revision of an Document.

 var map = "function() {emit(this.DocName, this.Revision);}";
 var reduce = "function (key,values) {return Math.max.apply(Math, values);}";
 QueryDocument Query = new QueryDocument("DocName", stFName);
 var mr = docs.MapReduce(Query, map, reduce).Response;

mr contains

{[{ "_id" : "WINWORD8.DOC", "value" : 2.0 }]}

How can开发者_C百科 use 'value' as an variable?

Regards

Thomas


Map reduce can be considered as overkill for such a simple request.

You can use the InlineResults property :

docs.MapReduce(Query, map, reduce).InlineResults

which is an enumerable of BsonDocument (in this case 1 doc only) and get the first result.

I would however suggest a better way to find the maximum revision, avoiding map reduce usage :

IMongoSortBy sort = SortBy.Descending("Revision");
IMongoQuery = Query.EQ("DocName", stFName);
BsonValue maxRev = docs.FindAs<BsonDocument>(q).SetFields(new string[] {"Revision"}).SetSortOrder(sort).SetLimit(1).GetFirstOrDefault()
if (maxRev !=null)
    int revMax = maxRev.AsBsonDocument.GetValue("Revision").AsInt32; // <- this is the maximum revision

If you want, in the end, to get the maximum revision for all your documents then mapreduce is a good call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜