Search Documents

Script mongodb Verified

by rossumblament ยท 6/6/2022

The script

Submitted by adam186 Deno
Verified 369 days ago
1
import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
2
import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
3

4
/**
5
 * @param data_source For example: `Cluster0`
6
 *
7
 * @param filter For example: `{ "_id": "01234" }`
8
 * Find out more at:
9
 * https://www.mongodb.com/docs/manual/reference/method/db.collection.find/
10
 */
11
type MongodbRest = {
12
  endpoint: string;
13
  api_key: string;
14
};
15
export async function main(
16
  auth: MongodbRest,
17
  data_source: string,
18
  database: string,
19
  collection: string,
20
  filter?: Record<string, any>,
21
  projection?: Record<string, number>,
22
  sort?: Record<string, number>,
23
  limit?: number,
24
  skip?: number,
25
) {
26
  const client = new MongoClient({
27
    endpoint: auth.endpoint,
28
    dataSource: data_source,
29
    auth: { apiKey: auth.api_key },
30
  });
31
  const documents = client.database(database).collection(collection);
32
  return await documents.find(
33
    filter,
34
    removeObjectEmptyFields({ projection, sort, limit, skip }),
35
  );
36
}
37

Other submissions
  • Submitted by rossmccrann Deno
    Created 1393 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
    @param: {object} filter - Object to filter mongodb documents
    25
    example:
    26
    { username: { $ne: null } }
    27
    
    
    28
    */
    29
    
    
    30
    export async function main(
    31
        mongodb_con: wmill.Resource<"mongodb">,
    32
        db_name: string,
    33
        collection_name: string,
    34
        filter: object
    35
    ) {
    36
        const client = new MongoClient();
    37
    
    
    38
        // Connecting to a Mongo Atlas Database
    39
        const resp = await client.connect(mongodb_con);
    40
    
    
    41
        const db = client.database(db_name);
    42
        const collection = db.collection(collection_name);
    43
    
    
    44
        const all_collection = await collection.find(filter).toArray();
    45
    
    
    46
        return (all_collection ? 'Documents Found in MongoDB' : 'Documents Not Found');
    47
    }