2016-05-09 17 views
0

最近、SSISのソースとしてMongoDBを使用し始めました(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スクリプトを作成することができた、と私は理解して、それは、C#では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#のにこのコードを変換することはできません。私はまだ選択基準を持っていて、くつろぎます。

助けてもらえますか?

答えて

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 =コレクション。?; – 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();