通过一些早期的帮助,我在SSIS中创建了一个C#脚本,用于从MongoDB到SQL Server检索数据。虽然可以轻松检索常规文档,但嵌套文档和数组存在问题。
问题1 :我有shipping_address.country,使用
返回结果this.UserDBBuffer.SCountry = document["shipping_address"].AsBsonDocument["country"].ToString();
然而, mlocation.address 给我一个错误'“country”找不到'使用相同的代码:
this.UserDBBuffer.Country = document["mlocation"].AsBsonDocument["country"].ToString();
问题2 :从数组中检索项目。我有一个看起来像“设备 - >文档 - > device_data - >模型”或“设备 - >文档 - > device_data - >品牌”的数组。如何在代码中检索“模型”或“品牌”值?
非常感谢你的帮助。以下是我的全部代码:
public override void CreateNewOutputRows()
{
string connectionString = "mongodb://localhost";
MongoServer myMongo = MongoServer.Create(connectionString);
myMongo.Connect();
var db = myMongo.GetDatabase("UserDB");
//Declaring variables for Date Created conversions
string DateCreatedString;
DateTime DateCreatedDateUTC;
DateTime DateCreatedDateLocal;
var fields = Fields.Include("mlocation.country", "mlocation", "_id", "primary_email", "gender", "date_created");
var collection = db.GetCollection<BsonDocument>("users");
foreach (var document in collection.FindAll().SetFields(fields))
{
this.UserDBBuffer.AddRow();
this.UserDBBuffer.ID = document["_id"] == null ? "" : document["_id"].ToString();
this.UserDBBuffer.Country = document["mlocation"].AsBsonDocument["country"].ToString();
this.UserDBBuffer.PrimaryEmail = document["primary_email"] == null ? "" : document["primary_email"].ToString();
this.UserDBBuffer.Gender = document["gender"] == null ? "" : document["gender"].ToString();
//Importing Date Created as String for data manipulation
DateCreatedString = document["date_created"] == null ? "" : document["date_created"].ToString();
//First, making sure that we have a UTC datetime
DateCreatedDateUTC = DateTime.Parse(DateCreatedString).ToUniversalTime();
//Second, converting to Local Time
DateCreatedDateLocal = DateTime.Parse(DateCreatedString).ToLocalTime();
//Finally, assigning variables to rows
this.UserDBBuffer.DateTimeCreatedUTC = DateCreatedDateUTC;
this.UserDBBuffer.DateTimeCreatedLocal = DateCreatedDateLocal;
}
myMongo.Disconnect();
}
对于问题2,我找到了某人使用的Java脚本;如果我可以将它转换为C#,它可能会有很大帮助:
count = 0;
function user_list(){
var cursor = db.users.find()
//var cursor = db.users.find({"devices": {"$ne":[]}})
cursor.forEach(function(user) {
var deviceInfo = "";
if (user.devices){
if (user.devices[0]){
dd = user.devices[0].device_data;
if (dd) {
deviceInfo = dd.model + "," + dd.brand + "," + dd.model + "," + dd.device + "," + dd.pixel_height + "," + dd.pixel_width + "," + dd.pixel_format;
}
}
}
var location = "";
if (user.mlocation) location = user.mlocation.country;
print(user._id + "," + location + "," + user.primary_email + "," + user.date_created + "," + deviceInfo);
count++;
});
}
user_list();
print(count);
答案 0 :(得分:1)
对于问题1,您确定所有文档都包含字段位置,该字段位置是包含国家/地区字段的文档。我能够重现未找到的&#34; Element国家&#34;使用缺少值的文档。 例如与
db.users.find() {&#34; _id&#34; :ObjectId(&#34; 4f04c56a0f8fa4413bed1078&#34;),&#34; primary_email&#34; :&#34; email@email.com",&#34; shipping_address&#34; :[{&#34; country&#34; :&#34; USA&#34;,&#34; city&#34; :&#34;旧金山&#34; },{&#34; country&#34; :&#34; IN&#34;,&#34; city&#34; :&#34; Chennai&#34; },&#34; mlocation&#34; :{&#34; country&#34; :&#34;加拿大&#34;,&#34;城市&#34; :&#34;蒙特利尔&#34; }} {&#34; _id&#34; :ObjectId(&#34; 4f04d1605ab5a3805aaa8666&#34;),&#34; primary_email&#34; :&#34; incorrect@email.com" ;,&#34; shipping_address&#34; :[{&#34; country&#34; :&#34; MX&#34;,&#34; city&#34; :&#34;卡波圣卢卡斯&#34; },&#34; mlocation&#34; :{&#34; city&#34; :&#34;蒙特利尔&#34; }} 第二个文件抛出异常。您可以检查其存在还是使用默认值选项 文件[&#34; mlocation&#34;]。AsBsonDocument.GetValue(&#34; country&#34;,null)
对于问题2,您不能将BsonArray强制转换为文档。所以对于上面的例子来获取shipping_address.country你可以做类似
的事情foreach (var addr in document["shipping_address"].AsBsonArray)
{
var country = addr.AsBsonDocument["country"].AsString;
}
答案 1 :(得分:0)
假设devices元素是一个数组,只需向下钻取到您要查找的元素,如下所示:
BsonDocument document; // assume this comes from somewhere
var devices = document["devices"].AsBsonArray;
var device = devices[0].AsBsonDocument; // first element of array
var deviceData = device["device_data"].AsBsonDocument;
var model = deviceData["model"].AsString;
var brand = deviceData["brand"].AsString;
为了清楚起见,我已将其细分为步骤,但如果您愿意,可以将其中一些步骤合并为更长的语句。
答案 2 :(得分:0)
为了澄清您对Robert的回答的评论,您可以使用BsonDocument.Contains检查给定的BsonDocument是否包含指定名称的字段,然后再获取其值(http://api.mongodb.org/csharp/current/html /6181f23f-f6ce-fc7d-25a7-fc682ffd3c04.htm)
答案 3 :(得分:0)
而不是:
var mlocation = document["mlocation"].AsBsonDocument;
var country = "";
if (mlocation != null && mlocation.Contains("country"))
{
country = mlocation.AsBsonDocument.GetValue("country").ToString();
}
我会写:
var mlocation = document["mlocation"].AsBsonDocument;
var country = "";
if (mlocation.Contains("country"))
{
country = mlocation["country"].AsString;
}
而不是:
var devices = document["devices"].AsBsonArray;
if (devices.ToList().Count > 0)
{
if (devices[0].AsBsonDocument != null)
{
var deviceinfo = devices[0].AsBsonDocument;
if (deviceinfo["device_data"].AsBsonDocument != null)
{
var deviceData = deviceinfo["device_data"].AsBsonDocument;
model = deviceData.GetValue("model", null).AsString;
}
}
}
我会写:
var devices = document["devices"].AsBsonArray;
if (devices.Count > 0)
{
var deviceinfo = devices[0].AsBsonDocument;
if (deviceinfo.Contains("device_data"))
{
var deviceData = deviceinfo["device_data"].AsBsonDocument;
var model = deviceData.GetValue("model", "").AsString; // "" instead of null
}
}