Wednesday, 19 September 2018

MongoDB Qqueries


 What is MongoDB?

1) MongoDB is an open-source, non-relational database developed by MongoDB, Inc.
2) MongoDB stores data as documents in a binary representation called BSON (Binary JSON).
3) Related information is stored together for fast query access through the MongoDB query language. 4) Fields can vary from document to document; there is no need to declare the structure of documents to the system – documents are self-describing. If a new field needs to be added to a document, then the field can be created without affecting all other documents in the collection, without updating a central system catalog, and without taking the system offline. Optionally, schema validation can be used to enforce data governance controls over each collection.
5) MongoDB’s document data model maps naturally to objects in application code, making it simple for developers to learn and use. Documents give you the ability to represent hierarchical relationships to store arrays and other more complex structures easily.
Native, idiomatic drivers are provided for 10+ languages – and the community has built dozens more – enabling ad-hoc queries, real-time aggregation and rich indexing to provide powerful programmatic ways to access and analyze data of any structure.
Because documents can bring together related data that would otherwise be modeled across separate parent-child tables in a relational schema, MongoDB’s atomic single-document operations already provide transaction semantics that meet the data integrity needs of the majority of applications.
6) In MongoDB one or more fields may be written in a single operation, including updates to multiple sub-documents and elements of an array. The guarantees provided by MongoDB ensure complete isolation as a document is updated; any errors cause the operation to roll back so that clients receive a consistent view of the document.
7) MongoDB 4.0 added support for multi-document transactions, making it the only open source database to combine the ACID guarantees of traditional relational databases, the speed, flexibility, and power of the document model, with the intelligent distributed systems design to scale-out and place data where you need it. Through snapshot isolation, transactions provide a consistent view of data, and enforce all-or-nothing execution to maintain data integrity. Transactions in MongoDB feel just like transactions developers are familiar with in MySQL. They are multi-statement, with similar syntax (e.g. start_transaction and commit_transaction), and therefore easy for anyone with prior transaction experience to add to any application.
8) Unlike MySQL and other relational databases, MongoDB is built on a distributed systems architecture, rather than a monolithic, single node design. As a result, MongoDB offers out-of-the-box scale-out and data localization with automatic sharding, and replica sets to maintain always-on availability.

 Terminology and Concepts

Many concepts in MySQL have close analogs in MongoDB. The table below outlines the common concepts across MySQL and MongoDB.

My SQL
Mongo DB
ACID Transactions
ACID Transactions
Table
Collection
Row
Document
Column
Field
Secondary Index
Secondary Index
JOINs
Embedded documents, $lookup & $graphLookup
GROUP_BY
Aggregation Pipeline
Some of examples of users collection:

1) db.users.insertOne( { _id: 10, item: "box", qty: 20 } )
2) db.users.insertMany( [
{ item: "card", qty: 15 },
{ item: "envelope", qty: 20 },
{ item: "stamps" , qty: 30 }
   ] );
}
3) db.users.find( {} )
4) db.users.find( { “username”:"swamy" } )
5) db.users.find( { “username”: { $in: [ "swamy", "raju" ] } } )
SELECT *FROM users WHERE username in ("swamy ", "raju")
6) db.users.find( { “username”: "swamy", “sal”: { $lt: 30000 } } )
SELECT * FROM users WHERE username = "swamy" AND sal< 30000
7)  db.users.updateOne(
      { "username" : "swamy" },
      { $set: { "sal" : 40000 } }
   );
8)  db.users.updateMany(
      { “sal”: { $gt: 40000 } },
      { $set: { "Review" : true } }
   );
9) db.users.updateMany(
   { },
   { $set: { "username" : “swamy” } }
)
10) db.users.replaceOne(
      { " username " : "swamy" },
      { " username " : " swamy ", "department" : "IT" }
   );
11) db.users.deleteOne( { "_id" : ObjectId("563237a41a4d68582c2509da") } );
12) db.orders.deleteOne( { "expiryts" : { $lt: ISODate("2015-11-01T12:40:15Z") } } );
13) db.orders.deleteMany( { "client" : "tcs" } );

CRUD operations create, read, update, and delete documents.
Create Operations
Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.

MongoDB provides the following methods to insert documents into a collection:
db.collection.insertOne() New in version 3.2
db.collection.insertMany() New in version 3.2

In MongoDB, insert operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.
For examples, see Insert Documents.

Read Operations
Read operations retrieves documents from a collection; i.e. queries a collection for documents. MongoDB provides the following methods to read documents from a collection:
You can specify query filters or criteria that identify the documents to return.
For examples, see:

Update Operations
Update operations modify existing documents in a collection. MongoDB provides the following methods to update documents of a collection:
db.collection.updateOne() New in version 3.2
db.collection.updateMany() New in version 3.2
db.collection.replaceOne() New in version 3.2
In MongoDB, update operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.
You can specify criteria, or filters, that identify the documents to update. These filters use the same syntax as read operations.
For examples, see Update Documents.

Delete Operations
Delete operations remove documents from a collection. MongoDB provides the following methods to delete documents of a collection:
db.collection.deleteOne() New in version 3.2
db.collection.deleteMany() New in version 3.2
In MongoDB, delete operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.
You can specify criteria, or filters, that identify the documents to remove. These filters use the same syntax as read operations.
For examples, see Delete Documents


4 comments: