Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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
将c#空格分隔的字符串转换为MongoDB BsonDocument_C#_Mongodb - Fatal编程技术网

将c#空格分隔的字符串转换为MongoDB BsonDocument

将c#空格分隔的字符串转换为MongoDB BsonDocument,c#,mongodb,C#,Mongodb,我成功地将BSONdocument上载到我的cloud MongoDB数据库,例如: var temp=新的BsonDocument { {“时间戳”,现在}, {“room1”,20}, {“room2”,24}, {“3号房间”,14} }; 然而,这只是一个测试,我真正的应用程序得到的是一个空格分隔的字符串,我仍然需要将其转换为这样的BsonDocument。我该怎么做?要转换的字符串如下所示: 字符串数据="政府0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

我成功地将BSONdocument上载到我的cloud MongoDB数据库,例如:

var temp=新的BsonDocument
{
{“时间戳”,现在},
{“room1”,20},
{“room2”,24},
{“3号房间”,14}
};
然而,这只是一个测试,我真正的应用程序得到的是一个空格分隔的字符串,我仍然需要将其转换为这样的BsonDocument。我该怎么做?要转换的字符串如下所示:

字符串数据="政府0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20.0 0.0 20.0 20.0-20.0 1 20.020.0 20.0 20.0 22.5 20.0 20.0 -20.0 1 15.9 0.0 0 0 2 -20 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 6 0 0 0 0 0 0 0 0 0 0 0 1 51 51 0000 0003 0000 0003 2000 0000 0000 0000"
BsonDocument(上例中为room1、room2,…)项目的左侧可以变成例如ID0、ID1、ID2。。。 谢谢

我要创建的对象的架构可能如下所示:

var whatineed=新的BsonDocument
{
{“时间戳”,现在},
{“id0”,49.8},
{“id1”,42.4},
{“id2”,2.2}
等等
};
根据需要,您可以传递一个json字符串,
BsonDocument
将发挥神奇的作用

所以,试试这一行(我没有太多地使用
String.Aggregate()
,所以可能存在更好的解决方案)

string json=“{{\'timestamp\”:“+DateTime.Now.ToString()+”},{\'id0\”:“+
data.Split(“”).Skip(1).Aggregate((pp,w)=>$“{pp}},{{{id{i++}\”:{w}”).TrimEnd(',')+“}”;
正如我所说,我不喜欢进入
String.Aggregate()
,所以我按空格分割,跳过第一个值,并拒绝添加
{}
和ID

这将使用您需要的模式创建json,然后

var document=BsonSerializer.Deserialize(json);
mongoCollection.Save(document…///您需要的任何内容

谢谢你的建议。我尝试了一些类似的方法,因为我的Linq知识太少,但效果很好:-)

Console.WriteLine(“正在尝试立即发送…”);
timestamp=DateTime.Now;
控制台写入线(时间戳);
字符串数据=“{'timestamp':'”+时间戳+”;
string[]split=read.split(“”);
如果(拆分长度>137)
{
对于(int i=0;i<(split.Length-2);i++)
{
数据+=”,'id“+i.ToString()+”:“+split[i+1]+”;
}
数据+=“}”;
var document=新的BsonDocument();
document.AddRange(BsonDocument.Parse(data));
发送(文件);
}

要创建的对象的架构如何?为要创建的对象添加了架构。
Console.WriteLine("Trying to send now...");
timestamp = DateTime.Now;
Console.WriteLine(timestamp);
string data = "{'timestamp':'" + timestamp + "'";
string[] split = read.Split(' ');
if (split.Length > 137)
{
   for (int i = 0; i < (split.Length-2); i++)
   {
       data += ", 'id" + i.ToString() + "':'" + split[i+1] + "'";
   }
   data += "}";                            
   var document = new BsonDocument();
   document.AddRange(BsonDocument.Parse(data));
   send(document);
}