2014-09-04 122 views
-2

我在mongodb中顯示如下所示的文檔。請讓我知道如何獲取最新的文檔,它與C#中的域名,主機名和服務(所有三個字段應該匹配)相匹配。查詢從Mongodb中獲取與所有列匹配的數據

{ 
    "_id" : ObjectId("53e1373e9c2e1805dd895cbd"), 
    "service" : "RAM__Usage", 
    "timestamp" : ISODate("2014-08-08T19:59:47.913Z"), 
    "hostname" : "HOSTXYZ", 
    "Domain" : "DOMXYZ" 
} 
{ 
    "_id" : ObjectId("53e1373e9c2e1805dd895abd"), 
    "service" : "RAM__Usage", 
    "timestamp" : ISODate("2014-09-09T19:59:47.913Z"), 
    "hostname" : "HOSTXYZ", 
    "Domain" : "DOMXYZ" 
} 
{ 
    "_id" : ObjectId("53e1373e9c2e1805dd895abe"), 
    "service" : "CPU__Usage", 
    "timestamp" : ISODate("2014-09-09T19:59:47.913Z"), 
    "hostname" : "HOSTXYZ", 
    "Domain" : "DOMXYZ" 
} 
{ 
    "_id" : ObjectId("53e1373e9c2e1805dd895aff"), 
    "service" : "RAM__Usage", 
    "timestamp" : ISODate("2014-06-06T19:59:47.913Z"), 
    "hostname" : "HOSTABC", 
    "Domain" : "DOMABC" 
} 
{ 
    "_id" : ObjectId("53e1373e9c2e1805dd895agc"), 
    "service" : "CPU__Usage", 
    "timestamp" : ISODate("2014-07-07T19:59:47.913Z"), 
    "hostname" : "HOSTABC", 
    "Domain" : "DOMABC" 
} 
+0

解決方案:VAR =查詢Query.And(Query.EQ( 「域」, 「DOMXYZ」),Query.EQ( 「主機名」, 「HOSTXYZ」), Query.EQ(「service」,「RAM_USAGE」)); (集合).Find(查詢).SetSortOrder(SortBy.Descending(「timestamp」))。SetLimit(1); GetMongoDatabase()。GetCollection Swamy 2014-09-08 10:55:06

回答

0

解決方案:

var query = Query.And(Query.EQ("Domain", "DOMXYZ"), Query.EQ("hostname", "HOSTXYZ"), Query.EQ("service", "RAM_USAGE")); 

    //Gets the latest record from MongoDB matching given fileds 
    var bsonCursor = GetMongoDatabase().GetCollection<YourClassName(YourMongoDBCollectionName).Find(query).SetSortOrder(SortBy.Descending("timestamp")).SetLimit(1); 

    foreach (YourClassName objYourClassName in bsonCursor) 
    { 
     //Do your task 
    } 
相關問題