我正在尝试使用指南针在字段上执行日期范围查询_id。我已经使用以下过滤器尝试了在这里找到的内容:
{_id: { $gte: ObjectId.fromDate(new Date('2019-01-01')) } }
Run Code Online (Sandbox Code Playgroud)
我缺少什么?我想获取从某个日期开始的所有文档的列表(在本例中是从 2019 年 1 月 1 日至今)。不幸的是,文档字段中没有时间戳,因此我需要从对象 ID 中提取它。
我用 junit 5 编写了一个单元测试,用于测试一些文件系统逻辑,我需要一个文件夹和一些文件。我在文档中找到了TempDir注释,并使用它创建了一个文件夹,在其中保存了一些文件。就像是:
@TempDir
static Path tempDir;
static Path tempFile;
// ...
@BeforeAll
public static void init() throws IOException {
tempFile = Path.of(tempDir.toFile().getAbsolutePath(), "test.txt");
if (!tempFile.toFile().createNewFile()) {
throw new IllegalStateException("Could not create file " + tempFile.toFile().getAbsolutePath());
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
在 junit4 中可以使用TemporaryFolder#newFile(String)。junit5 中似乎没有这个。
我错过了什么吗?它可以工作,所以我想这很好,但我想知道是否有一种更干净的方法可以直接使用 junit 5 api 创建新文件。