2016-05-09 72 views
0

我最近开始使用MongoDB作为SSIS中的源代码(使用C#驱动程序)。我对MongoDB和C#非常新。 当我没有嵌套的文件,如下面的语句为我工作:

var query = Query.And(Query.Or(Query.GT("CreatedOn",maxUpdatedOnBSON), Query.GT("UpdatedOn", maxUpdatedOnBSON)), 
      Query.Or(Query.LT("CreatedOn", cutoffDate), Query.LT("UpdatedOn", cutoffDate)),Query.In("TestType", testTypes)); 

    MongoCursor<BsonDocument> toReturn = collection.Find(query); 

现在,我得到了嵌套的文件。我能够创建Java脚本,并将其与MongoDB的本身

db.Test.aggregate([ 
{ $unwind : { path: "$Items",includeArrayIndex: "arrayIndex"} } , 
{ $match: { $and: [ 
     {$or: [ { CreatedOn: { $gt: ISODate("2015-11-22T00:00:00Z")} }, {UpdatedOn: { $gt: ISODate("2015-11-22T00:00:00Z") } } ] }, 
     {$or: [ { CreatedOn: { $lt: ISODate("2016-05-09T00:00:00Z")} }, {UpdatedOn: { $lt: ISODate("2016-05-09T00:00:00Z") } } ] } 
        ] } 
}]) 

工作在C#中,我明白了,我必须使用的,而不是找到,但我不能把这种代码转换为C#。我仍然有选择标准和放松。

你能帮忙吗?

回答

0

因为没有发布集合模板,我附上了一些类似于您要查找的代码段。这有帮助吗?

 var builder = Builders<BsonDocument>.Filter; 
     //and operator can be used similar to below by using operator "&" or builder.And. 

     var filter = builder.Eq("state", "nj") | builder.Eq("state", "CO"); 
     var filter2 = builder.Eq("pop", 6033) | builder.Eq("city", "nyc"); 
     filter = builder.And(filter, filter2); 
     var pipeline = grades.Aggregate() 
      .Unwind(x => x["Items"]) 
      .Match(filter); 


     var list = pipeline.ToList(); 

     foreach (var item in list) 
     { 
      //do something 
     } 
+0

我希望如此。将尝试这种方法,谢谢。问题是关于MongoCursor 语法。 MongoCursor toReturn = collection。?; – ICHV

+0

好,如果你想要一个聚合的游标,你需要聚合或将聚合选项传递给聚合构件。或上面编辑的答案。你不能让MongoCursor离开它。你可以像这样获得IAsyncCursor var aggCursor = grades.Aggregate() .Unwind(x => x [“Items”]) .Match(filter).ToCursor(); – KaSh

+0

看看这个链接的详细信息asynccursor http://stackoverflow.com/questions/29682371/how-is-an-iasynccursor-used-for-iteration-with-the-mongodb-c-sharp-driver? RQ = 1 – KaSh

0

我得到的帮助和共享解决方案:

//Create matching criteria used in the aggregation pipeline to bring back only the specified documents based on date range 

    var match = new BsonDocument("$match", 
      new BsonDocument("$and", 
       new BsonArray() 
       .Add(new BsonDocument("$or", new BsonArray().Add(new BsonDocument("CreatedOn", new BsonDocument("$gt", maxUpdatedOnBSON))).Add(new BsonDocument("UpdatedOn", new BsonDocument("$gt", maxUpdatedOnBSON))))) 
       .Add(new BsonDocument("$or", new BsonArray().Add(new BsonDocument("CreatedOn", new BsonDocument("$lt", cutoffDate))).Add(new BsonDocument("UpdatedOn", new BsonDocument("$lt", cutoffDate))))))); 

    //create the arguments to pass to the $unwind method of the aggregation 
    var unwindargs = new BsonDocument("path", "$LineItems"); 
    unwindargs.Add("includeArrayIndex", "arrayIndex"); 

    //create the unwind stage and add the arguments 
    var unwind = new BsonDocument("$unwind", unwindargs); 

    //create a new pipeline and gather the results 
    var pipeline = new[] { match, unwind }; 
    var mongoArgs = new AggregateArgs { Pipeline = pipeline }; 

    var toReturn = collection.Aggregate(mongoArgs).ToList();