我最近开始使用MongoDB作为SSIS中的源(使用C#驱动程序)。我是MongoDB和C#的新手。 当我没有嵌套文档时,下面的语句对我有用:
git cherry-pick <commit-hash of version 15 from testing branch>
现在,我获得了嵌套文档。我能够创建java脚本,它可以与MongoDB本身一起使用
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);
在C#中,据我所知,我必须使用聚合而不是查找,但我无法将此代码转换为C#。我仍然有选择标准和放松。
你能帮忙吗?
答案 0 :(得分: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
}
答案 1 :(得分: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();