我在SSIS中创建了一个脚本来从MongoDB中检索数据。虽然我没有任何问题查询常规文档,但我不确定如何从嵌套文档检索值。例如,展开的“地址”包含“国家”,“州”,“城市”,“街道”和“邮编”。我只想检索“国家”(字段)值。理论上,我知道它应该像“Address.Country”,但我不知道如何在代码中实现它。什么是实现这一目标的最佳方式?从MongoDB查询嵌套对象
这是检索所有其他文件的代码:
public override void CreateNewOutputRows()
{
string connectionString = "mongodb://localhost";
MongoServer myMongo = MongoServer.Create(connectionString);
myMongo.Connect();
var db = myMongo.GetDatabase("UserDB");
/*ICursor<BsonDocument> cursor = db.GetCollection<BsonDocument>("UserDB").FindAll();*/
foreach (BsonDocument document in db.GetCollection<BsonDocument>("UserDB").FindAll())
{
this.UserDBBuffer.AddRow();
this.UserDBBuffer.ID = document["_id"] == null ? "" : document["_id"].ToString();
this.UserDBBuffer.PrimaryEmail = document["primary_email"] == null ? "" : document["primary_email"].ToString();
this.UserDBBuffer.Gender = document["gender"] == null ? "" : document["gender"].ToString();
}
}
谢谢!这正是我需要的! – Pasha 2012-01-04 04:37:48
现在,下一个挑战是如何从数组中提取数据。例如,其中一个阵列如下所示:“devices - > Document - > device_data - > model”或“devices - > Document - > device_data - > brand”。我如何提取设备的型号或品牌? 再次感谢您的帮助! – Pasha 2012-01-04 04:40:36
我将“地址”更改为“位置”(另一个字段包含国家/地区),我的脚本返回“国家/地区未找到”,我找不到原因...... – Pasha 2012-01-04 05:34:29