# MongoDB
::linux ::js
A document-oriented database replaces the concept of a “row” with a more flexible model, the “document.”
- Schemaless
[DB-Engines Ranking - popularity ranking of database management systems](https://db-engines.com/en/ranking)
* A database shard is a horizontal partition of data in a database or search engine. Each shard is held on a separate database server instance, to spread load.
* Today, the term "shard" refers to the deployment and use of redundant hardware across database systems.
# Document (row, record)
a data structure that is a natural fit, such as a map, hash, or dictionary. In JavaScript, for example, documents are represented as objects:
{"greeting" : "Hello, world!"}
* special key, "_id", that is unique within a collection.
* Binary JSON, or BSON
# Collections (table with dynamic schema)
is a group of documents.

• Grouping documents of the same kind together in the same collection allows for data locality.
• It’s much faster to get a list of collections than to extract a list of the types of documents in a collection.
• Keeping different kinds of documents in the same collection can be a nightmare
• By putting only documents of a single type into the same collection, we can index our collections more efficiently.
[MongoDB CRUD Operations — MongoDB Manual](https://docs.mongodb.com/manual/crud/)
## C : insert
```
use myNewDB
db.myNewCollection1.insertOne( { x: 1 } )
db.myNewCollection2.createIndex( { a: 1 } )
db.test.insertOne( { ts: new Timestamp(), data: Math.sin(0.1) } );
```
## R : find

```
db.movies.find()
```
```
SELECT * FROM movies
```
## U : update

# D : delete
db.inventory.deleteMany({ status : "A" })
# Databases
# mongo shell
* query
* js
```
show dbs
```
# Rich with Features
* Indexing
* generic secondary indexes and provides
* unique, compound, geospatial, and
* full-text indexing capabilities as well.
* Aggregation (data processing pipelines)
* Special collection and index types
* time-to-live (TTL) collections for data that should expire
* File storage
# Sharding data (collection)
* distributing documents in a collection across the shards in a cluster.
* scale-out architecture that supports even the largest applications.
# Full-text search
means searching through all the terms of all the documents available in the database
* This is quite different from simple SQL queries run against columns of type string in relational databases. Normal SQL queries with a WHERE clause and an equals (=) or LIKE clause try to do an exact or wildcard match with underlying data. SQL queries can, at best, just match the search term to a sub-string within the text column.
# A pipeline
defines a series of processors. Each processor transforms the document in some way. Each processor is executed in the order in which it is defined in the pipeline. A pipeline consists of two main fields: a description and a list of processors
# Transactions
* logical groups of processing in a database,
# ACID
ACID is the accepted set of properties a transaction must meet to be a “true” transac‐ tion.
## Atomicity
all of the commands that make up a transaction are treated as a single unit and either succeed or fail together.
## Consistency
changes made within a transaction are consistent with database constraints.
## Isolation
concurrent transactions do not affect each other’s outcomes.
## Durability
once the database has told the client it has written the data, the data has in fact been written to a backing store. The data will persist even in the case of a system failure.
___
# Run it in Container!
[mongod — MongoDB Manual](https://docs.mongodb.com/manual/reference/program/mongod/)
```
mkdir -p data/db
chown -R $USER:$USER /data/db
podman run --name mongodb -d -v $(pwd)/data/db:/data/db:Z -p 27017:27017 mongo
podman exec -it mongodb mongosh
show dbs
use mydb
show collections
doc = { hello: 'world' }
db.mydb.insertOne(doc);
for(let i=0; i<5; i++) db.mydb.insertOne({i:i});
db.mydb.find({i: {$exists: true }} )
db.mydb.find( { i: { $eq: 5 } } )
```
___
# Understand the purpose of NoSQL and MongoDB
[SQL Fiddle](http://sqlfiddle.com/#!9/78968d/8)
___
# MongoDB Fundamentals
## An Introduction to ETL Operations and the Aggregation Framework
Axel Sirota
[MongoDB: Getting Started with CRUD Operations](https://learning.oreilly.com/scenarios/mongodb-getting-started/9781098100506/)
# Elasticsearch
Elasticsearch is a real-time distributed search and analytics engine.
- explore your data at a speed and at a scale
- full-text search,
- structured search,
- analytics,
- and all three in combination:
- Apache Lucene™ is a high-performance, full-featured text search engine library written entirely in Java.
[1. Getting Started · madhusudhankonda/elasticsearch-first-steps Wiki · GitHub](https://github.com/madhusudhankonda/elasticsearch-first-steps/wiki/1.-Getting-Started)
[Home · madhusudhankonda/elasticsearch-first-steps Wiki · GitHub](https://github.com/madhusudhankonda/elasticsearch-first-steps/wiki)
{"metaMigratedAt":"2023-06-16T12:27:41.135Z","metaMigratedFrom":"Content","title":"MongoDB","breaks":true,"contributors":"[{\"id\":\"45d7dfdf-2725-40c2-873c-18445f631747\",\"add\":5519,\"del\":334}]"}