如何从mongodb获取特定的嵌入式数组并使用mongodb c#驱动程序将其附加到新文档中

时间:2019-01-21 06:21:03

标签: mongodb-.net-driver

我想从mongodb文档中获取特定的嵌入式数组,并使用“ mongodb.Driver” .net驱动程序在该嵌入式数组中添加新文档。

将文档插入为:

{
  "_id": "5c41b5c6b0ce0437dc576c53",
  "ProjectId": "234",
  "OwnerId": "62",
  "ProjectName": "proj4h46m",
  "FileDetails": [
    {
      "TotalWord": "-1",
      "RepeatedWord": "-1",
      "TMWordCount": "-1",
      "TranslationRequired": "-1",
      "ParentFileName": "test",
      "ChildFileName": "test_AR-SA",
      "Status": "Newly Uploaded"
    }
  ]
}

我希望从中获取“ FileDetails”数组,并添加新文档并更新到mongodb。如下所示:

{
  "_id": "5c41b5c6b0ce0437dc576c53",
  "ProjectId": "234",
  "OwnerId": "62",
  "ProjectName": "proj4h46m",
  "FileDetails": [
    {
      "TotalWord": "-1",
      "RepeatedWord": "-1",
      "TMWordCount": "-1",
      "TranslationRequired": "-1",
      "ParentFileName": "test",
      "ChildFileName": "test_AR-SA",
      "Status": "Newly Uploaded"
    },
    {
      "TotalWord": "10",
      "RepeatedWord": "3",
      "TMWordCount": "12",
      "TranslationRequired": "1",
      "ParentFileName": "test2",
      "ChildFileName": "test_AR-KSA",
      "Status": "Newly Uploaded"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

我是通过以下方法获得的:-

 var query2 = Query.EQ(""ProjectId", "234");
 var document=@"{""TotalWord"": ""10"",""RepeatedWord"": "3",""TMWordCount"": ""12"",""TranslationRequired"": ""1"",""ParentFileName"": ""test2"",""ChildFileName"": ""test_AR-KSA"",""Status"": ""Newly Uploaded""}";
 var update = Update.Push("FileDetails", document.ToBsonDocument());
 collec.Update(query2, update);