Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建包含$redact的c#mongodb管道_C#_Mongodb - Fatal编程技术网

如何创建包含$redact的c#mongodb管道

如何创建包含$redact的c#mongodb管道,c#,mongodb,C#,Mongodb,我正在尝试使用C#驱动程序创建一个mongodb聚合管道,该驱动程序包括一个项目后面的修订。我尝试了以下几种方法,但在每种情况下都只执行管道的第一阶段。AppendStage似乎不会追加下一个阶段。那么,如何在使用C#mongodb驱动程序的项目之后进行修订呢。请注意,fluent界面并不直接支持编校,但另一篇文章使用下面的代码进行了演示,该代码适用于第一阶段 我使用的是2.4.3版本的C#驱动程序和mongodb版本3.4.4 string redactJson = System.IO.Fil

我正在尝试使用C#驱动程序创建一个mongodb聚合管道,该驱动程序包括一个项目后面的修订。我尝试了以下几种方法,但在每种情况下都只执行管道的第一阶段。AppendStage似乎不会追加下一个阶段。那么,如何在使用C#mongodb驱动程序的项目之后进行修订呢。请注意,fluent界面并不直接支持编校,但另一篇文章使用下面的代码进行了演示,该代码适用于第一阶段

我使用的是2.4.3版本的C#驱动程序和mongodb版本3.4.4

string redactJson = System.IO.File.ReadAllText(@"redactTest.json");
string projectJson = System.IO.File.ReadAllText(@"projectTest.json");

var collection = Database.GetCollection<BsonDocument>("Forecasts");

var redact = BsonDocument.Parse(redactJson);
var project = BsonDocument.Parse(projectJson);


var aggregatonPipeline = collection.Aggregate();
aggregatonPipeline.AppendStage<BsonDocument>(redact);
aggregatonPipeline.AppendStage<BsonDocument>(project);

var list = aggregatonPipeline.ToList();
projectTest.json

{
  "$project":
  {
    "_id": 0,
    "title": 1,
    "year": 1,
    "subsections.subtitle": 1,
    "subsections.content":  1 
  }
}
源文档是

{
  _id: 1,
  title: "123 Department Report",
  tags: [ "G", "STLW" ],
  year: 2014,
  subsections: [
    {
      subtitle: "Section 1: Overview",
      tags: [ "SI", "G" ],
      content:  "Section 1: This is the content of section 1."
    },
    {
      subtitle: "Section 2: Analysis",
      tags: [ "STLW" ],
      content: "Section 2: This is the content of section 2."
    },
    {
      subtitle: "Section 3: Budgeting",
      tags: [ "TK" ],
      content: {
      text: "Section 3: This is the content of section3.",
       tags: [ "HCS" ]
     }
   }
 ]
}
collection.Aggregate()
公开fluent聚合接口,并通过方法链接将阶段附加到管道中

差不多

var pipeline= collection.Aggregate().AppendStage<BsonDocument>(redact).AppendStage<BsonDocument>(project);
var list = pipeline.ToList();
var pipeline=collection.Aggregate();
var list=pipeline.ToList();

每次添加一个阶段时,您的使用会覆盖以前的阶段

这很有效。我试图证明每个阶段,然后附加它们不是一个好主意。
{
  _id: 1,
  title: "123 Department Report",
  tags: [ "G", "STLW" ],
  year: 2014,
  subsections: [
    {
      subtitle: "Section 1: Overview",
      tags: [ "SI", "G" ],
      content:  "Section 1: This is the content of section 1."
    },
    {
      subtitle: "Section 2: Analysis",
      tags: [ "STLW" ],
      content: "Section 2: This is the content of section 2."
    },
    {
      subtitle: "Section 3: Budgeting",
      tags: [ "TK" ],
      content: {
      text: "Section 3: This is the content of section3.",
       tags: [ "HCS" ]
     }
   }
 ]
}
var pipeline= collection.Aggregate().AppendStage<BsonDocument>(redact).AppendStage<BsonDocument>(project);
var list = pipeline.ToList();