1

Create New Document

by
Published Jun 6, 2022
Script mongodb Verified

The script

Submitted by hugo989 Bun
Verified 6 days ago
1
import { MongoClient } from "mongodb@6";
2

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

15
/**
16
 * Create new document
17
 * Insert a single document into a collection.
18
 */
19
export async function main(
20
  auth: Mongodb,
21
  collection: string,
22
  document: Record<string, any>,
23
  database?: string,
24
) {
25
  const client = await mongoClient(auth);
26
  try {
27
    return await client
28
      .db(database || auth.db)
29
      .collection(collection)
30
      .insertOne(document);
31
  } finally {
32
    await client.close();
33
  }
34
}
35

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

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
    2
    
    
    3
    /**
    4
     * @param data_source For example: `Cluster0`
    5
     */
    6
    type MongodbRest = {
    7
      endpoint: string;
    8
      api_key: string;
    9
    };
    10
    export async function main(
    11
      auth: MongodbRest,
    12
      data_source: string,
    13
      database: string,
    14
      collection: string,
    15
      document: Record<string, any>,
    16
    ) {
    17
      const client = new MongoClient({
    18
        endpoint: auth.endpoint,
    19
        dataSource: data_source,
    20
        auth: { apiKey: auth.api_key },
    21
      });
    22
      const documents = client.database(database).collection(collection);
    23
      return await documents.insertOne(document);
    24
    }
    25
    
    
  • Submitted by rossmccrann Deno
    Created 1422 days ago
    1
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { Bson, MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
    3
    
    
    4
    /*
    5
    @param: {wmill.Resource} mongodb - Resource containing mongodb connection object
    6
    example:
    7
    await client.connect({
    8
      db: "<db_name>",
    9
      tls: true,
    10
      servers: [
    11
        {
    12
          host: "<db_cluster_url>",
    13
          port: 27017,
    14
        },
    15
      ],
    16
      credential: {
    17
        username: "<username>",
    18
        password: "<password>",
    19
        db: "<db_name>",
    20
        mechanism: "SCRAM-SHA-1",
    21
      },
    22
    });
    23
    */
    24
    
    
    25
    export async function main(
    26
        mongodb_con: wmill.Resource<"mongodb">,
    27
        new_document: object,
    28
        db_name: string,
    29
        collection_name: string,
    30
    ) {
    31
        const client = new MongoClient();
    32
    
    
    33
        // Connecting to a Mongo Atlas Database
    34
        const resp = await client.connect(mongodb_con);
    35
    
    
    36
        const db = client.database(db_name);
    37
        const coll = db.collection(collection_name);
    38
    
    
    39
        const insertId = await coll.insertOne(new_document);
    40
    
    
    41
        return `Document inserted in MongoDB`;
    42
    }
    43