Get recently inserted documents

Trigger script to get the newly added documents in a collection.

Script· trigger mongodb Verified

by adam186 · 12/21/2022

The script

Submitted by hugo989 Bun
Verified 4 days ago
1
import { MongoClient, ObjectId } from "mongodb@6";
2
import { getState, setState } from "windmill-client@1";
3

4
type Mongodb = {
5
  servers: { host: string; port: number }[];
6
  credential: {
7
    username: string;
8
    password: string;
9
    db: string;
10
    mechanism?: string;
11
  };
12
  db: string;
13
  tls: boolean;
14
};
15

16
/**
17
 * Get recently inserted documents
18
 * Return documents inserted since the last run, using the timestamp embedded in their ObjectId `_id`.
19
 */
20
export async function main(
21
  auth: Mongodb,
22
  collection: string,
23
  database?: string,
24
) {
25
  const lastCheck: number = (await getState()) ?? 0;
26
  await setState(Date.now() / 1000);
27

28
  const client = await mongoClient(auth);
29
  try {
30
    return await client
31
      .db(database || auth.db)
32
      .collection(collection)
33
      .find({ _id: { $gt: ObjectId.createFromTime(Math.floor(lastCheck)) } })
34
      .toArray();
35
  } finally {
36
    await client.close();
37
  }
38
}
39

40
async function mongoClient(auth: Mongodb) {
41
  const hosts = auth.servers.map((s) => `${s.host}:${s.port}`).join(",");
42
  const options: any = { tls: auth.tls };
43
  if (auth.credential?.username) {
44
    options.auth = {
45
      username: auth.credential.username,
46
      password: auth.credential.password,
47
    };
48
    options.authSource = auth.credential.db;
49
    if (auth.credential.mechanism) {
50
      options.authMechanism = auth.credential.mechanism;
51
    }
52
  }
53
  const client = new MongoClient(`mongodb://${hosts}`, options);
54
  await client.connect();
55
  return client;
56
}
57

Other submissions
  • Submitted by adam186 Deno
    Created 396 days ago
    1
    import {
    2
      getState,
    3
      setState,
    4
    } from "https://deno.land/x/[email protected]/mod.ts";
    5
    import {
    6
      MongoClient,
    7
      ObjectId,
    8
    } from "https://deno.land/x/[email protected]/mod.ts";
    9
    
    
    10
    /**
    11
     * @param data_source For example: `Cluster0`
    12
     */
    13
    type MongodbRest = {
    14
      endpoint: string;
    15
      api_key: string;
    16
    };
    17
    export async function main(
    18
      auth: MongodbRest,
    19
      data_source: string,
    20
      database: string,
    21
      collection: string,
    22
    ) {
    23
      const client = new MongoClient({
    24
        endpoint: auth.endpoint,
    25
        dataSource: data_source,
    26
        auth: { apiKey: auth.api_key },
    27
      });
    28
      const documents = client.database(database).collection(collection);
    29
      const lastCheck = (await getState()) || 0;
    30
      await setState(Date.now() / 1000);
    31
      const id = ObjectId.createFromTime(lastCheck);
    32
      return await documents.find({ _id: { $gt: id } });
    33
    }
    34