Edits history of script submission #260 for ' Create Many New Documents (mongodb)'

  • deno
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param data_source For example: `Cluster0`
     */
    type MongodbRest = {
      endpoint: string;
      api_key: string;
    };
    export async function main(
      auth: MongodbRest,
      data_source: string,
      database: string,
      collection: string,
      documents: Record<string, any>[],
    ) {
      const client = new MongoClient({
        endpoint: auth.endpoint,
        dataSource: data_source,
        auth: { apiKey: auth.api_key },
      });
      const docs = client.database(database).collection(collection);
    
      let items: Record<string, any>[];
      try {
        items = documents.map((doc) =>
          typeof doc === "string" ? JSON.parse(doc) : doc,
        );
      } catch (err) {
        throw Error(`\nReceived array of strings in the 'documents' argument.
    Attempted to parse them into objects but the process failed with the following error:\n${err}`);
      }
    
      return await docs.insertMany(items);
    }
    

    Submitted by hugo697 378 days ago

  • deno
    import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param data_source For example: `Cluster0`
     */
    type MongodbRest = {
      endpoint: string;
      api_key: string;
    };
    export async function main(
      auth: MongodbRest,
      data_source: string,
      database: string,
      collection: string,
      documents: Record<string, any>[],
    ) {
      const client = new MongoClient({
        endpoint: auth.endpoint,
        dataSource: data_source,
        auth: { apiKey: auth.api_key },
      });
      const docs = client.database(database).collection(collection);
    
      let items: Record<string, any>[];
      try {
        items = documents.map((doc) =>
          typeof doc === "string" ? JSON.parse(doc) : doc,
        );
      } catch (err) {
        throw Error(`\nReceived array of strings in the 'documents' argument.
    Attempted to parse them into objects but the process failed with the following error:\n${err}`);
      }
    
      return await docs.insertMany(items);
    }
    

    Submitted by admin 1011 days ago

  • deno
    import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param data_source For example: `Cluster0`
     */
    type MongodbRest = {
      endpoint: string;
      api_key: string;
    };
    export async function main(
      auth: MongodbRest,
      data_source: string,
      database: string,
      collection: string,
      documents: Record<string, any>[]
    ) {
      const client = new MongoClient({
        endpoint: auth.endpoint,
        dataSource: data_source,
        auth: { apiKey: auth.api_key },
      });
      const docs = client.database(database).collection(collection)
    
      let items: Record<string, any>[]
      try {
        items = documents.map(doc => typeof doc === 'string' ? JSON.parse(doc) : doc)
      } catch(err) {
        throw Error(`\nReceived array of strings in the 'documents' argument.
    Attempted to parse them into objects but the process failed with the following error:\n${err}`)
      }
    
      return await docs.insertMany(items)
    }
    

    Submitted by admin 1014 days ago

  • deno
    import { Resource } from "https://deno.land/x/[email protected]/mod.ts";
    import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param data_source For example: `Cluster0`
     */
    export async function main(
      auth: Resource<"mongodb_rest">,
      data_source: string,
      database: string,
      collection: string,
      documents: Record<string, any>[]
    ) {
      const client = new MongoClient({
        endpoint: auth.endpoint,
        dataSource: data_source,
        auth: { apiKey: auth.api_key },
      });
      const docs = client.database(database).collection(collection)
    
      let items: Record<string, any>[]
      try {
        items = documents.map(doc => typeof doc === 'string' ? JSON.parse(doc) : doc)
      } catch(err) {
        throw Error(`\nReceived array of strings in the 'documents' argument.
    Attempted to parse them into objects but the process failed with the following error:\n${err}`)
      }
    
      return await docs.insertMany(items)
    }
    

    Submitted by adam186 1142 days ago

  • deno
    import { Resource } from "https://deno.land/x/[email protected]/mod.ts";
    import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param data_source For example: `Cluster0`
     */
    export async function main(
      auth: Resource<"mongodb_rest">,
      data_source: string,
      database: string,
      collection: string,
      documents: Record<string, any>[]
    ) {
      const client = new MongoClient({
        endpoint: auth.endpoint,
        dataSource: data_source,
        auth: { apiKey: auth.api_key },
      });
      const docs = client.database(database).collection(collection)
    
      let items: Record<string, any>[]
      try {
        items = documents.map(doc => typeof doc === 'string' ? JSON.parse(doc) : doc)
      } catch(err) {
        throw Error(`\nReceived array of strings in the 'documents' argument.
    Attempted to parse them into objects but the process failed with the following error:\n${err}`)
      }
    
      return await docs.insertMany(items)
    }
    

    Submitted by adam186 1177 days ago

  • deno
    import { Resource } from "https://deno.land/x/[email protected]/mod.ts";
    import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param data_source For example: `Cluster0`
     */
    export async function main(
      auth: Resource<"mongodb_rest">,
      data_source: string,
      database: string,
      collection: string,
      documents: Record<string, any>[]
    ) {
      const client = new MongoClient({
        endpoint: auth.endpoint,
        dataSource: data_source,
        auth: { apiKey: auth.api_key },
      });
      const docs = client.database(database).collection(collection)
    
      let items: Record<string, any>[]
      try {
        items = documents.map(doc => typeof doc === 'string' ? JSON.parse(doc) : doc)
      } catch(err) {
        throw Error(`\nReceived array of strings in the 'documents' argument.
    Attempted to parse them into objects but the process failed with the following error:\n${err}`)
      }
    
      return await docs.insertMany(items)
    }
    

    Submitted by adam186 1246 days ago