Delete a Document

Script mongodb Verified

by armancejamstvena ยท 6/6/2022

The script

Submitted by adam186 Deno
Verified 370 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
 * @param filter For example: `{ "_id": "01234" }`
7
 * Find out more at:
8
 * https://www.mongodb.com/docs/manual/reference/method/db.collection.deleteOne/
9
 */
10
type MongodbRest = {
11
  endpoint: string;
12
  api_key: string;
13
};
14
export async function main(
15
  auth: MongodbRest,
16
  data_source: string,
17
  database: string,
18
  collection: string,
19
  filter: Record<string, any>,
20
) {
21
  const client = new MongoClient({
22
    endpoint: auth.endpoint,
23
    dataSource: data_source,
24
    auth: { apiKey: auth.api_key },
25
  });
26
  const documents = client.database(database).collection(collection);
27
  return await documents.deleteOne(filter);
28
}
29

Other submissions
  • Submitted by rossmccrann Deno
    Created 1355 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
    export async function main(
    6
        mongodb_con: wmill.Resource<"mongodb">,
    7
        deleteId: number,
    8
        database: string,
    9
        collection: string
    10
    ) {
    11
        const client = new MongoClient();
    12
    
    
    13
        // Connecting to a Mongo Atlas Database
    14
        const resp = await client.connect(mongodb_con);
    15
    
    
    16
        const db = client.database(database);
    17
        const users = db.collection(collection);
    18
    
    
    19
        const deleteCount = await users.deleteOne({ _id: deleteId });
    20
    
    
    21
        return 'Document deleted in MongoDB';
    22
    }