1
Update a Document
One script reply has been approved by the moderators Verified
Created by stephanesbei 689 days ago Viewed 5192 times
0
Submitted by adam186 Deno
Verified 492 days ago
1
import { MongoClient } from "https://deno.land/x/atlas_sdk@v1.0.3/mod.ts";
2

3
/**
4
 * @param data_source For example: `Cluster0`
5
 *
6
 * @param filter For example: `{ "_id": "01234" }`
7
 *
8
 * @param upsert If `true` and no documents match the `filter`,
9
 * then a new document will be created with the values of `document`.
10
 * Default is `false`.
11
 */
12
type MongodbRest = {
13
  endpoint: string;
14
  api_key: string;
15
};
16
export async function main(
17
  auth: MongodbRest,
18
  data_source: string,
19
  database: string,
20
  collection: string,
21
  filter: Record<string, any>,
22
  document: Record<string, any>,
23
  upsert?: boolean,
24
) {
25
  const client = new MongoClient({
26
    endpoint: auth.endpoint,
27
    dataSource: data_source,
28
    auth: { apiKey: auth.api_key },
29
  });
30
  const docs = client.database(database).collection(collection);
31
  upsert = typeof upsert === "boolean" ? upsert : undefined;
32
  return await docs.updateOne(filter, document, { upsert });
33
}
34

Other submissions