An open API service indexing awesome lists of open source software.

https://github.com/trstringer/mongodb-by-example

:leaves: Learn MongoDB through examples
https://github.com/trstringer/mongodb-by-example

Last synced: 3 months ago
JSON representation

:leaves: Learn MongoDB through examples

Awesome Lists containing this project

README

          

# Learn MongoDB by Examples

*Learn MongoDB through examples*

#### General tasks
- [create test data](/test-data/test-data.js) for the purpose of running these examples
- [set database context](#set-database-context)

#### Read data
- [read entire collection](#read-entire-collection)
- [read certain fields from the collection](#read-certain-fields-from-the-collection)
- [search documents with search parent field](#search-documents-with-search-parent-field)
- [search documents with search embedded field](#search-documents-with-search-embedded-field)
- [search documents with search parent embedded field and display embedded fields](#search-documents-with-search-parent-embedded-field-and-display-embedded-fields)

#### Update data
- [update documents with search predicate](#update-documents-with-search-predicate)
- [update documents with embedded search predicate](#update-documents-with-embedded-search-predicate)
- [increment or decrement document fields](#increment-or-decrement-document-fields)

#### Delete data
- [delete all documents in the collection](#delete-all-documents-in-the-collection)
- [delete all documents that match a predicate](#delete-all-documents-that-match-a-predicate)

## Set database context

#### MongoDB (shell)

```javascript
use yourDatabase;
```

## Read entire collection

#### MongoDB (shell)

```javascript
db.myCollection.find();

// or for prettified output
db.myCollection.find().pretty();
```

## Read certain fields from the collection

#### MongoDB (shell)

```javascript
db.myCollection.find({}, { firstName: 1, lastName: 1 }); // .pretty()

// and to NOT select the default _id field
db.myCollection.find({}, { _id: 0, firstName: 1, lastName: 1 }); // .pretty()
```

## Search documents with search parent field

#### MongoDB (shell)

```javascript
db.myCollection.find(
{ firstName: 'Thomas' }
); // .pretty()
```

## Search documents with search embedded field

#### MongoDB (shell)

```javascript
db.myCollection.find(
{ 'address.city': 'Atlanta' }
); // .pretty()
```

## Search documents with search parent embedded field and display embedded fields

#### MongoDB (shell)

```javascript
db.myCollection.find(
{ 'address.city': 'Atlanta' },
{
_id: 0,
firstName: 1,
lastName: 1,
'address.city': 1,
'address.state': 1
}
); // .pretty()
```

## Update documents with search predicate

#### MongoDB (shell)

```javascript
db.myCollection.update(
{ lastName: 'Stringer' },
{
$set: {
firstName: 'Thomas'
}
}
);
```

## Update documents with embedded search predicate

#### MongoDB (shell)

```javascript
db.myCollection.update(
{ 'address.city': 'Atlanta' },
{ $set: { 'address.city': 'ATL' }}
);
```

## Increment or decrement document fields

#### MongoDB (shell)

```javascript
db.myCollection.update(
{ firstName: 'Alexis', lastName: 'Andrews' },
{
$inc: {
age: 1
}
}
);
```

## Delete All Documents in the Collection

#### MongoDB (shell)

```javascript
db.myCollection.remove({});
```

## Delete All Documents that match a Predicate

#### MongoDB (shell)

```javascript
db.myCollection.remove({firstName: 'Thomas'});
```