Created by stephanesbei 293 days ago Viewed 112 times 1 Point
No comments yet
import { Resource } from "https://deno.land/x/windmill@v1.70.1/mod.ts";
import { MongoClient } from "https://deno.land/x/atlas_sdk@v1.0.3/mod.ts";
/**
* @param data_source For example: `Cluster0`
*
* @param filter For example: `{ "_id": "01234" }`
*
* @param upsert If `true` and no documents match the `filter`,
* then a new document will be created with the values of `document`.
* Default is `false`.
*/
export async function main(
auth: Resource<"mongodb_rest">,
data_source: string,
database: string,
collection: string,
filter: Record<string, any>,
document: Record<string, any>,
upsert?: boolean
) {
const client = new MongoClient({
endpoint: auth.endpoint,
dataSource: data_source,
auth: { apiKey: auth.api_key },
});
const docs = client.database(database).collection(collection)
upsert = typeof upsert === 'boolean' ? upsert : undefined
return await docs.updateOne(filter, document, { upsert })
}
No comments yet
import * as wmill from "https://deno.land/x/windmill@v1.19.2/mod.ts";
import { Bson, MongoClient } from "https://deno.land/x/mongo@v0.30.1/mod.ts";
/*
@param: {wmill.Resource} mongodb - Resource containing mongodb connection object
example:
await client.connect({
db: "<db_name>",
tls: true,
servers: [
{
host: "<db_cluster_url>",
port: 27017,
},
],
credential: {
username: "<username>",
password: "<password>",
db: "<db_name>",
mechanism: "SCRAM-SHA-1",
},
});
*/
export async function main(
mongodb_con: wmill.Resource<"mongodb">,
db_name: string,
collection_name: string,
data: object,
_id: string
) {
const client = new MongoClient();
// Connecting to a Mongo Atlas Database
const resp = await client.connect(mongodb_con);
const db = client.database(db_name);
const collection = db.collection(collection_name);
const { matchedCount, modifiedCount, upsertedId } = await collection.updateOne(
{ _id: ObjectID(_id), },
{ $set: data },
);
return ({ matchedCount: matchedCount, modifiedCount: modifiedCount, upsertedId: upsertedId });
}
No comments yet