diff --git a/src/tools/index.ts b/src/tools/index.ts
index e8b3380b..5bae8178 100644
--- a/src/tools/index.ts
+++ b/src/tools/index.ts
@@ -44,6 +44,7 @@ import { tool as certificateKeyParser } from './certificate-key-parser';
import { tool as crcCalculator } from './crc-calculator';
import { tool as ipGeoLocation } from './ip-geo-location';
import { tool as safelinkDecoder } from './safelink-decoder';
+import { tool as mongoObjectidConverter } from './mongo-objectid-converter';
import { tool as xmlToJson } from './xml-to-json';
import { tool as jsonToXml } from './json-to-xml';
import { tool as regexTester } from './regex-tester';
@@ -273,6 +274,7 @@ export const toolsByCategory: ToolCategory[] = [
jqMemo,
jsonpathMemo,
jsonToSchema,
+ mongoObjectidConverter,
],
},
{
diff --git a/src/tools/mongo-objectid-converter/index.ts b/src/tools/mongo-objectid-converter/index.ts
new file mode 100644
index 00000000..1a9a0fc7
--- /dev/null
+++ b/src/tools/mongo-objectid-converter/index.ts
@@ -0,0 +1,12 @@
+import { Database } from '@vicons/tabler';
+import { defineTool } from '../tool';
+
+export const tool = defineTool({
+ name: 'MongoDB ObjectId Converter',
+ path: '/mongo-objectid-converter',
+ description: 'Convert between MongoDB ObjectId and internal timestamp',
+ keywords: ['mongo', 'objectid', 'converter', 'timestamp'],
+ component: () => import('./mongo-objectid-converter.vue'),
+ icon: Database,
+ createdAt: new Date('2024-08-15'),
+});
diff --git a/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.test.ts b/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.test.ts
new file mode 100644
index 00000000..28075aee
--- /dev/null
+++ b/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it } from 'vitest';
+import { dateFromObjectId, generateMongoFilter, objectIdFromDate, objectIdSyntaxFromDate } from './mongo-objectid-converter.service';
+
+describe('mongo-objectid-converter', () => {
+ describe('objectIdFromDate', () => {
+ it('convert a Date to ObjectId', () => {
+ expect(objectIdFromDate(new Date(Date.UTC(2024, 0, 1)))).to.eql('659200800000000000000000');
+ expect(objectIdFromDate(new Date(Date.UTC(2024, 0, 1, 12, 12, 12)))).to.eql('6592ac1c0000000000000000');
+ });
+ });
+ describe('objectIdSyntaxFromDate', () => {
+ it('convert a Date to ObjectId', () => {
+ expect(objectIdSyntaxFromDate(new Date(Date.UTC(2024, 0, 1)))).to.eql('ObjectId("659200800000000000000000")');
+ });
+ });
+ describe('dateFromObjectId', () => {
+ it('convert an ObjectId to Date', () => {
+ expect(dateFromObjectId('659200800000000000000000')).to.eql(new Date(Date.UTC(2024, 0, 1)));
+ expect(dateFromObjectId('6592ac1c0000000000000000')).to.eql(new Date(Date.UTC(2024, 0, 1, 12, 12, 12)));
+ });
+ });
+ describe('generateMongoFilter', () => {
+ it('convert a date to mongo query', () => {
+ expect(generateMongoFilter({
+ date: new Date(Date.UTC(2024, 0, 1, 12, 12, 12)),
+ tableName: 'comments',
+ })).to.eql('db.comments.find({_id: {$gt: ObjectId("6592ac1c0000000000000000")}})');
+ });
+ });
+});
diff --git a/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.ts b/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.ts
new file mode 100644
index 00000000..e3ac5c06
--- /dev/null
+++ b/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.ts
@@ -0,0 +1,23 @@
+export function objectIdFromDate(date: Date) {
+ return `${Math.floor(date.getTime() / 1000).toString(16)}0000000000000000`;
+};
+
+export function objectIdSyntaxFromDate(date: Date) {
+ return `ObjectId("${objectIdFromDate(date)}")`;
+};
+
+export function generateMongoFilter(
+ {
+ tableName, date, operator = 'gt',
+ }:
+ {
+ tableName: string
+ date: Date
+ operator?: 'gt' | 'lt'
+ }) {
+ return `db.${tableName}.find({_id: {$${operator}: ObjectId("${objectIdFromDate(date)}")}})`;
+}
+
+export function dateFromObjectId(objectId: string) {
+ return new Date(Number.parseInt(objectId.substring(0, 8), 16) * 1000);
+};
diff --git a/src/tools/mongo-objectid-converter/mongo-objectid-converter.vue b/src/tools/mongo-objectid-converter/mongo-objectid-converter.vue
new file mode 100644
index 00000000..825e7bf1
--- /dev/null
+++ b/src/tools/mongo-objectid-converter/mongo-objectid-converter.vue
@@ -0,0 +1,68 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+