如何使用C#中ObjectId中的时间戳过滤文档?
我正在构建一个需要将数据从MongoDB文档传输到SQL Server表的应用程序。我正在创建一个JSON文件,用于将MongoDB文档导出到其中(代码随附)。 我现在如何添加一个过滤器,以便在将特定数据重新导出到JSON后,仅在MongoDB集合中创建文档 我相信这可以通过在MongoDB文档的ObjectId字段中使用时间戳来实现,但无法找到方法如何使用C#中ObjectId中的时间戳过滤文档?,c#,mongodb,C#,Mongodb,我正在构建一个需要将数据从MongoDB文档传输到SQL Server表的应用程序。我正在创建一个JSON文件,用于将MongoDB文档导出到其中(代码随附)。 我现在如何添加一个过滤器,以便在将特定数据重新导出到JSON后,仅在MongoDB集合中创建文档 我相信这可以通过在MongoDB文档的ObjectId字段中使用时间戳来实现,但无法找到方法 using (FileStream fs = File.Create(path)) { using (var fw = new Strea
using (FileStream fs = File.Create(path))
{
using (var fw = new StreamWriter(fs))
{
fw.Write("[");
using (var cursor = await collection.Find(new BsonDocument()).Project(Builders<BsonDocument>.Projection.Exclude("_id")).ToCursorAsync())
{
while (await cursor.MoveNextAsync())
foreach (var doc in cursor.Current)
{
fw.Write(doc.ToString());
fw.Write(",");
}
fw.Flush();
}
fs.SetLength(fs.Length - 1);
fw.Write("]");
}
}
使用(FileStream fs=File.Create(path))
{
使用(var fw=新StreamWriter(fs))
{
fw.写(“[”);
使用(var cursor=await collection.Find(new BsonDocument()).Project(Builders.Projection.Exclude(“\u id”)).ToCursorAsync())
{
while(等待cursor.MoveNextAsync())
foreach(游标中的var doc.Current)
{
fw.Write(doc.ToString());
fw.写(“,”);
}
fw.Flush();
}
fs.SetLength(fs.Length-1);
fw.填写(“]);
}
}
我不能使用您的确切示例,但我已经成功创建了类似的东西,使用ObjectId datetime进行过滤:
// Declare a date range - presumably these would be dynamic not fixed strings
var startDateTime = DateTime.Parse("2018-09-13 14:19:26.000Z");
var endDateTime = DateTime.Parse("2018-09-24 14:03:38.000Z");
// Use the dates to create ObjectId type for comparison
var startId = new ObjectId(startDateTime, 0, 0, 0);
var endId = new ObjectId(endDateTime, 0, 0, 0);
// Use the ObjectId types in the filter
using (var cursor = await collection.Find(x => x._id > startId && x._id < endId).ToCursorAsync())
{
while (await cursor.MoveNextAsync())
{
foreach (var doc in cursor.Current)
{
// Use doc object
}
}
}
//声明一个日期范围-这些可能是动态的而不是固定的字符串
var startDateTime=DateTime.Parse(“2018-09-13 14:19:26.000Z”);
var endDateTime=DateTime.Parse(“2018-09-2414:03:38.000Z”);
//使用日期创建用于比较的ObjectId类型
var startId=新的ObjectId(startDateTime,0,0,0);
var endId=newobjectid(endDateTime,0,0,0);
//使用过滤器中的ObjectId类型
使用(var cursor=await collection.Find(x=>x.\u id>startId&&x.\u id
注意:我在上面的代码片段中使用了最新的MongoDB.Driver包,正在使用StreamWriter构建一个JSON文件,然而,可以通过C#应用程序代码使用mongoexport.exe进程来解决这个问题,这也使得过滤更容易:
public static string dateConverter(DateTime dt)
{
long decimalNumber = (long)(dt.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
return (Convert.ToString(decimalNumber, 16));
}
public static void Main(string[] args)
{
try
{
CultureInfo provider = CultureInfo.InvariantCulture;
string instr;
Console.WriteLine("Enter the start date");
instr = Console.ReadLine();
DateTime.TryParseExact(instr, "yyyy/MM/dd", provider, DateTimeStyles.None, out startDate);
Console.WriteLine("Enter the end date");
instr = Console.ReadLine();
DateTime.TryParseExact(instr, "yyyy/MM/dd", provider, DateTimeStyles.None, out endDate);
queryFilter = "{_id:{$gte: ObjectId('" + dateConverter(startDate) + "0000000000000000'), $lte: ObjectId('" + dateConverter(endDate) + "ffffffffffffffff')}}";
string expstring = " --db yourDatabaseName --collection yourCollectionName --type json --query " + queryFilter + " --out yourFilePath --jsonArray";
Process export = new Process();
export.StartInfo.FileName = ExportEXEPath;
export.StartInfo.Arguments = expstring;
export.Start();
}
catch (Exception ex)
{
Console.WriteLine("[ERROR]: " + ex.Message);
}
}
但是,在使用本身包含双引号(“)的字符串将参数传递到命令行时存在一个问题(正如在--query关键字之后所做的那样),可以通过单击来参考关于它的讨论。看看。如果这不是您要找的,则非常相似。