Skip to content

Commit

Permalink
chore: update README (#282)
Browse files Browse the repository at this point in the history
* chore: update README
  • Loading branch information
erfanium authored Oct 22, 2021
1 parent 73654d0 commit 1ace025
Showing 1 changed file with 69 additions and 23 deletions.
92 changes: 69 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@
## Links

- [Docs](https://doc.deno.land/https/deno.land/x/mongo/mod.ts)
- [Guides]() TODO
- [Examples]() TODO
- [Benchmarks]() TODO

## Examples

### Import

replace `LATEST_VERSION` with current latest version

```ts
import { Bson, MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
import {
Bson,
MongoClient,
} from "https://deno.land/x/mongo@LATEST_VERSION/mod.ts";
```

### Connect

```ts
const client = new MongoClient();

//Connecting to a Local Database
// Connecting to a Local Database
await client.connect("mongodb://localhost:27017");

//Connecting to a Mongo Atlas Database
// Connecting to a Mongo Atlas Database
await client.connect({
db: "<db_name>",
tls: true,
Expand All @@ -42,11 +51,15 @@ await client.connect({
},
});

//Or
// Connect using srv url
await client.connect(
"mongodb+srv://<username>:<password>@<db_cluster_url>/<db_name>?authMechanism=SCRAM-SHA-1",
);
```

### Access Collection

```ts
// Defining schema interface
interface UserSchema {
_id: { $oid: string };
Expand All @@ -56,14 +69,16 @@ interface UserSchema {

const db = client.database("test");
const users = db.collection<UserSchema>("users");
```

### Insert

// insert
```ts
const insertId = await users.insertOne({
username: "user1",
password: "pass1",
});

// insertMany
const insertIds = await users.insertMany([
{
username: "user1",
Expand All @@ -74,52 +89,83 @@ const insertIds = await users.insertMany([
password: "pass2",
},
]);
```

### Find

// findOne
```ts
const user1 = await users.findOne({ _id: insertId });

// find
const all_users = await users.find({ username: { $ne: null } }).toArray();

// find by ObjectId
const user1_id = await users.findOne({
_id: new Bson.ObjectId("SOME OBJECTID STRING"),
});
```

// count
const count = await users.count({ username: { $ne: null } });
### Count

// aggregation
```ts
const count = await users.countDocuments({ username: { $ne: null } });

const estimatedCount = await users.estimatedDocumentCount({
username: { $ne: null },
});
```

### Aggregation

```ts
const docs = await users.aggregate([
{ $match: { username: "many" } },
{ $group: { _id: "$username", total: { $sum: 1 } } },
]);
```

### Update

// updateOne
```ts
const { matchedCount, modifiedCount, upsertedId } = await users.updateOne(
{ username: { $ne: null } },
{ $set: { username: "USERNAME" } },
);

// updateMany
const { matchedCount, modifiedCount, upsertedId } = await users.updateMany(
{ username: { $ne: null } },
{ $set: { username: "USERNAME" } },
);
```

// deleteOne
### Delete

```ts
const deleteCount = await users.deleteOne({ _id: insertId });

// deleteMany
const deleteCount2 = await users.deleteMany({ username: "test" });
```

### Cursor methods

// Skip
const skipTwo = await users.skip(2).find();
```ts
const cursor = users.find();

// Limit
const featuredUser = await users.limit(5).find();
// Skip & Limit
cursor.skip(10).limit(10);

// GridFS Upload
// toArray
const users = await cursor.toArray();

// iterate
for await (const user of cursor) {
console.log(user);
}
```

### GridFS

```ts
// Upload
const bucket = new GridFSBucket(db);
const upstream = bucket.openUploadStream("test.txt");

Expand All @@ -128,7 +174,7 @@ writer.write(fileContents);

await writer.close();

//GridFS Download
// Download
const file = await new Response(bucket.openDownloadStream(id)).text();
```

Expand Down

0 comments on commit 1ace025

Please sign in to comment.