• MongoDB


    https://www.w3resource.com/mongodb/nosql.php

    Introduction To NoSQL

    Classical Relational Database Follow The ACID Rules

    Atomic, Consistent, Isolated, Durable

    Distributed Systems

    A distributed system consists of multiple computers and software components that communicate through a computer network.

    Advantages

    Reliability, Scalability, Sharing of Resources, Flexibility, Speed, Open system, Performance

    Disadvantages

    Troubleshooting, Software, Networking, Security

    Scalability

    Vertical Scaling

    To scale vertically (or scale up) means to add resources within the same logical unit to increase capacity.

    Horizontal Scaling

    To scale horizontally (or scale out) means to add more nodes to a system, such as adding a new computer to a distributed software application.

    What is NoSQL

    NoSQL is a non-relational database management systems. It is designed for distributed data stores where very large scale of data can be stored. These type of data storing may not require fixed schema, avoid join operations and typically scale horizontally.

    Why NoSQL

    The data has been increasing exponentially. SQL database were not designed to process huge amount of data. The evolution of NoSQL databases is to handle these huge data properly.

    RDBMS vs NoSQL

    RDBMS

    • Structured and organized data
    • Structured query language (SQL)
    • Data and its relationships are stored in separate tables.
    • Data Manipulation Language, Data Definition Language

    NoSQL

    • Stands for Not Only SQL
    • No declarative query language
    • No predefined schema
    • Key-Value pair storage, Column Store, Document Store, Graph databases
    • Eventual consistency rather ACID property
    • Unstructured and unpredictable data
    • CAP theorem
    • Prioritizes high performance, high availability and scalability
    • BASE Transaction

    CAP Theorem

    Consistency, Availability, Partition Tolerance
    CA(RDBMS), CP(MongoDB, Redis), AP(Cassandra)

    NoSQL pros/cons

    Advantages

    • High scalability
    • Distributed Computing
    • Lower cost
    • Schema flexibility, semi-structure data
    • No complicated Relationships

    Disadvantages

    • No standardization
    • Limited query capabilities (so far)
    • Eventual consistent is not intuitive to program for

    The BASE

    A BASE system gives up on consistency.

    • Basically Available
    • Soft state
    • Eventual consistency

    NoSQL Categories

    Key-value stores

    Redis

    Column-oriented database

    Cassandra

    Graph databases

    OrientDB

    Document-oriented database

    MongoDB

    Introduction to MongoDB

    MongoDB stores data as documents. So it is a document oriented database.

    • MongoDB is a document-oriented DBMS, with JSON-like objects comprising the data model. MongoDB does not support joins nor transactions.
    • MongoDB uses dynamic schemas. We can create collections without defining the structure. You can change the structure of documents simply by adding new fields or deleting existing ones.
    • MongoDB database stores its data in collections not in tables. The collections are the rough equivalent of RDBMS tables.

    Introduction to Mongo Shell

    Usage of multi-line operations

    If a line in mongo shell ends with an open parenthesis (‘(’) or an open brace (‘{’), or an open bracket (‘[’), then the subsequent lines start with ellipsis (“…”) until you enter the corresponding closing parenthesis (‘)’) or the closing brace (‘}’) or the closing bracket (‘]’).
    ##Access different databases temporarily

    use mytest
    db.getSiblingDB(‘sampleDB’).getCollectionNames();
    ##Tab completion and other keyboard shortcuts.

    • Use the up/down arrow keys to scroll through command history.
    • Use to autocomplete or to list the completion possibilities.

    MongoDB: Data Types

    Double, String, Object, Array, Binary data, Object id, Boolean, Date, Null, Regular Expression, JavaScript, Symbol, JavaScript(with scope), 32-bit integer, Timestamp, 64-bit integer, Min key, Max key

    Databases, documents and collections in MongoDB

    Databases

    “show dbs” command provides you with a list of all the databases.
    Run “db” command to refer to the current database object or connection.
    To connect to a particular database, run use command

    Documents

    The document is the unit of storing data in a MongoDB database. Document use JSON style for storing data. Often, the term “object” is used to refer a document.

    Collections

    A collection may store a number of documents. A collection is analogous to a table of an RDBMS. A collection may store documents those who are not same in structure. This is possible because MongoDB is a Schema-free database.
    Using a “.” (dot) notation, collections can be organized in named groups.
    Capped collections are collections which can store data in the same order it is inserted.

    Metadata

    Information about a database is stored in certain collections. They are grouped in system namespace, as dbname.system.*

    MongoDB Connections

    Start MongoDB Server

    To start a MongoDB Server, you have to execute ‘mongod’ from bin folder of your MongoDB installation folder.
    By default, MongoDB starts at port 27017 and MongoDB web interface is at port 1000 more than that. Browsing http://localhost:28017, you can see MongoDB web interface.

    Connection to MongoDB Server from shell

    Execute “mongodb://localhost”.
    “mongodb://mongo_admin:Ax86_w3r”@localhost/w3r”, with that command, ‘mongo_admin’ user with a password of ‘AxB6_w3r’ is connected to ‘w3r’ database at localhost.
    mongodb://mongo_admin:AxB6_w3r@localhost/w3r:29000
    mongodb://example_host1.com:27017,example_host2.com:27017
    mongodb://example_host1.com:27110,example_host1.com:27111

    Query and Projection Operators

    Comparison Query Operators

    $gt, $lt, $gte, $lte, $ne, $in, KaTeX parse error: Expected '}', got 'EOF' at end of input: …e.find({age : {gt : 22}}).pretty();
    db.testtable.find({age : {KaTeX parse error: Expected 'EOF', got '}' at position 8: lt : 19}̲}).pretty(); …lte : 19}}).pretty();
    db.testtable.find({age : {$lt :24, KaTeX parse error: Expected 'EOF', got '}' at position 8: gt : 17}̲}).pretty(); …ne :19}})
    db.testtable.find({“age” : { KaTeX parse error: Expected 'EOF', got '}' at position 19: …: [19,20,22,25]}̲}) db.testtab…nin : [“vinod”,“janal”,“nonoj”,“anant”]}})

    Logical Query Operators

    $and, $not, $or, KaTeX parse error: Expected '}', got 'EOF' at end of input: ….student.find({and:[{“sex”:“Male”},{“grd_point”:{ $gte: 31 }},{“class”:“VI”}]}).pretty();
    db.student.find( {“age”: { KaTeX parse error: Expected '}', got 'EOF' at end of input: not: {lt : 12}}}).pretty();
    db.student.find( {“sex”: { KaTeX parse error: Expected 'EOF', got '}' at position 12: not: /^M.*/}̲}).pretty(); …or : [{“age” : 19},{“age” : 22},{“age”:23}]});
    db.testtable.find( { “date_of_join” : “16/10/2010” , $nor : [{“age” : 19},{“age” : 22},{“age”:23}]})

    Element Query Operators

    $exists, $type
    db.testtable.find({“extra.community_members” : { $exists : true } } ).pretty();
    db.testtable.find({“extra.community_members” : { KaTeX parse error: Expected 'EOF', got '}' at position 16: exists : false }̲ } ).pretty(); …type : 3}}).pretty();

    Evaluation Query Operators

    $mod, $regex, KaTeX parse error: Expected '}', got 'EOF' at end of input: …find({"age" : {mod : [8,0]}}).pretty();
    db.testtable.find({“age” : {$mod : [5,2]}}).pretty();
    db.student.find( { f_name: { $regex: ‘P.'} } ).pretty();
    db.student.find( { f_name: { $regex: 'p.
    ’, $options: ‘i’ } } ).pretty();
    db.student.find( { f_name: /p/i } ).pretty();
    db.student.find( { f_name: /P/ } ).pretty();
    db.student.find( { f_name: /^p/i } ).pretty();
    db.table3.find( { $where: function() { return (this.english == this.science) }}).pretty();
    db.table3.find( { $where: function() { return (obj.english == obj.science)}}).pretty();
    db.table3.find( “this.english == this.science”).pretty();

    Query Operator Array

    $all, $elemMatch, KaTeX parse error: Expected '}', got 'EOF' at end of input: …ty_members" : {all : [5000,1500]}}).pretty();
    db.table1.find( { “description”: { $elemMatch: { “agegroup” : “3-5”,“price” : 5}}}).pretty();
    db.table1.find( { “description”: { KaTeX parse error: Expected '}', got 'EOF' at end of input: …0-13","price":{gte:7}}}}).pretty();
    db.testtable.find({“extra.friends.valued_friends_id” : {KaTeX parse error: Expected 'EOF', got '}' at position 9: size : 3}̲}).pretty(); …size : 3}}).sort({“age”:-1}).pretty();

    Projection Operators

    $, $elemMatch, $slice
    db.mediitem.find( { “statement.qty”: { KaTeX parse error: Expected 'EOF', got '}' at position 9: gt: 200 }̲ },{ "statement…": 1 }).sort( { “statement.qty”: 1 } ).pretty();
    db.mediitem2.find( { batch: 10452 },{ tran_details: { $elemMatch: { prate: 50 } } } ).pretty();
    db.table2.find ({ “student_id”:“STU001” }, { “score”: { $slice: 3}}).pretty();
    db.table2.find ({}, { “score”: { $slice: 3}}).pretty();

    Update Operators

    Field Update Operators

    $inc, $rename, $set, $setOnInsert, $unset
    db.items.update( { item_id: “I001” },{ $inc: { qty: 10 }});
    db.items.update( { “item_id”: “I001” }, { $inc: { “tax%”: 2 } }, { multi: true } );
    db.employee.update( { emp_id: 1232 }, { $rename: { ‘offinfo’: ‘service_details’, ‘emale’: ‘email’ } } );
    db.employee.update( { emp_id: 1232 }, { $rename: { “personal.emplname”: “pinfo.last_name”} } );
    db.employeeset.update( { emp_id: 1231 },{ KaTeX parse error: Expected 'EOF', got '}' at position 43: …ytestmail.com"}}̲); db.employe…gte:2} },{ $set: {“block”: “E”}},{multi : true});
    db.items1.update({ _id: 1 },{ s e t O n I n s e r t : " o p s t o c k " : 200 , " d e s c r i p t i o n " : " i t e m 2 " , setOnInsert: {"op_stock": 200,"description":"item2" }, setOnInsert:"opstock":200,"description":"item2",set: {“purqty”: 100}},{ upsert: true });
    db.items1.update( { _id: 1 },{ KaTeX parse error: Expected 'EOF', got '}' at position 22: … {"purqty": ""}}̲); db.items1.…gte:100}}, { $unset: {“purqty”: “”}},{ multi: true });

    Array Update Operators

    $addToSet, $pop, $pull, $pullAll, $push
    db.student.update( { “sem”: 1}, { $addToSet: { “achieve”: 92 } } );
    db.student.update( { “sem”: 1}, { KaTeX parse error: Expected '}', got 'EOF' at end of input: …: { "achieve":{each:[10,11]}}});
    db.student.update( {achieve: 70 }, { $pop: { achieve : -1 } } );
    db.student.update( { “subjects” : “maths” }, { $pull: { “subjects”: “maths” }} );
    db.student.update( { “subjects” : “gkn” }, { $pull: { “achieve”: 90 }} );
    db.student.update( { “subjects” : “gkn” }, { KaTeX parse error: Expected '}', got 'EOF' at end of input: …: { "achieve":{gte :85} }});
    db.student.update( { “subjects” : “gkn” }, { $pullAll: { “achieve”: [65,87,90] }} );
    db.student.update( { “subjects” : “gkn” },{ $push: { “achieve”: 95 } });
    db.student.update( { “subjects” : “gkn” },{ KaTeX parse error: Expected '}', got 'EOF' at end of input: … { "achieve": {each : [77,49,83 ]} } });

    Aggregation Pipeline Operators

    Stage Operators

    $project, $match, $redact, $unwind, $group, $out
    db.empmast.aggregate( [ { $project : { emp_code : 1 , emp_name : 1 } } ] );
    db.empdetails.aggregate( [ { $project: { “deduction.pf”: 1 } } ] ); or db.empdetails.aggregate( [ { KaTeX parse error: Expected 'EOF', got '}' at position 35: …n: { pf: 1 } } }̲ ] ); db.empd…project: {emp_code: 1,date_of_join: {day: { s u b s t r : [ " substr: [ " substr:["date_of_join", 0, 2 ] },month: { s u b s t r : [ " substr: [ " substr:["date_of_join", 3, 2 ] },year: { s u b s t r : [ " substr: [ " substr:["date_of_join", 6, 4 ] },},PF: “ d e d u c t i o n . p f " , S a l a r y : " deduction.pf",Salary: " deduction.pf",Salary:"salary”}}]);
    db.empdetails.aggregate( [ { $match : { salary : 9000 } } ]).pretty();
    db.empdetails.aggregate( [{ $match : { salary : { $gt : 9000, $lte : 12000 } } },{ $group: { _id: null, count: { $sum: 1 } } }]);
    db.test1.aggregate( [ { u n w i n d : " unwind : " unwind:"sizes" } ] );
    db.invoice.aggregate( [{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "inv_date",totalCost: { $sum: { m u l t i p l y : [ " multiply: [ " multiply:["rate", “$qty” ] } },avgQty: { a v g : " avg: " avg:"qty" },count: { $sum: 1 }}}]).pretty();
    db.invoice.aggregate( [{ KaTeX parse error: Expected '}', got 'EOF' at end of input: …oup : { _id : "item", invoiceDate: { p u s h : " push: " push:"inv_date" } } },{ $out : “newinvoice” }] );

    Set Operators

    $setEquals, $setIntersection, $setUnion, $setDifference, $setIsSubset, $anyElementTrue, $allElementsTrue
    db.test_collection.aggregate( [{ $project: { A: 1, B: 1, sameElements: { s e t E q u a l s : [ " setEquals: [ " setEquals:["A", “$B” ] }, _id: 0 } }]);
    db.test_collection.aggregate([{ $project: { A: 1, B: 1, commonToBoth: { s e t I n t e r s e c t i o n : [ " setIntersection: [ " setIntersection:["A", “$B” ] }, _id: 0 } }]);
    db.test_collection.aggregate([{ $project: { A:1, B: 1, allValues: { s e t U n i o n : [ " setUnion: [ " setUnion:["A", “$B” ] }, _id: 0 } }]);
    db.test_collection.aggregate([{ $project: { A: 1, B: 1, inBOnly: { s e t D i f f e r e n c e : [ " setDifference: [ " setDifference:["B", “$A” ] }, _id: 0 } }]);
    db.test_collection.aggregate([{ $project: { A:1, B: 1, AisSubset: { s e t I s S u b s e t : [ " setIsSubset: [ " setIsSubset:["A", “$B” ] }, _id:0 } }]);
    db.survey_collection.aggregate( [ { $project: { replies: 1, isAnyTrue: { a n y E l e m e n t T r u e : [ " anyElementTrue: [ " anyElementTrue:["replies" ] }, _id: 0 } }]);
    db.survey_collection.aggregate([{ $project: { replies: 1, isAllTrue: { a l l E l e m e n t s T r u e : [ " allElementsTrue: [ " allElementsTrue:["replies" ] }, _id: 0 } }]);

    count()

    db.testtable.count()
    db.testtable.find({“sex”:“Male”}).skip(1).count(true)
    #Database Commands

    User Management Commands

    { createUser: “”,pwd: “”,customData: { },roles: [{ role: “”, db: “” } | “”,],writeConcern: { }}
    { usersInfo: { user: , db: },showCredentials: ,showPrivileges: }
    db.runCommand({usersInfo: { user: “myNewuser”, db: “userdetails” },showPrivileges: false})
    { updateUser: “”,pwd: “<cleartext password&glt;”,customData: { },roles: [ { role: “”, db: “” } | “”,],writeConcern: { }}
    {dropUser: “”,writeConcern: { }}
    {dropAllUsersFromDatabase: 1, writeConcern: { }
    { grantRolesToUser: “”,roles: [ ],writeConcern: { }}
    { grantRolesToUser: “”,roles: [ ],writeConcern: { }}
    { revokeRolesFromUser: “”,roles: [{ role: “”, db: “” } | “”,],writeConcern: { }}

    Authentication commands

    db.runCommand({logout:1})
    db.auth( “myNewuser”, “thisPassword” );
    #MongoDB INSERT
    db.userdetails.insert(document)
    #MongoDB UPDATE
    db.userdetails.update({“user_id” : “QRSTBWN”},{“user_id” : “QRSTBWN”,“password” :“NEWPASSWORD” ,“date_of_join” : “17/10/2010” ,“education” :“M.B.A.” , “profession” : “MARKETING”,“interest” : “MUSIC”,“community_name” :[“MODERN MUSIC”, “CLASSICAL MUSIC”,“WESTERN MUSIC”],“community_moder_id” : [“MR. BBB”,“MR. JJJ”,“MR MMM”],“community_members” : [500,200,1500],“friends_id” : [“MMM123”,“NNN123”,“OOO123”],“ban_friends_id” :[“BAN123”,“BAN456”,“BAN789”]});
    #MongoDB DELETE
    db.userdetails.remove( { “user_id” : “testuser” } );
    db.userdetails.remove({});
    db.userdetails.drop();
    db.dropDatabase();
    #MongoDB INDEX
    db.userdetails.find().sort({“education”:1})
    db.userdetails.find().sort({“education”:1,“password”:-1})
    db.empinfo.ensureIndex( { “emp_id” : 1 } );
    db.system.indexes.find();

    MongoDB Shell Methods

    Collection

    db.collection.aggregate(),db.collection.count, db.collection.createIndex(), db.collection.dataSize(), db.collection.distinct(), db.collection.drop(), db.collection.dropIndex(), db.collection.dropindexes(), db.collection.explain(), db.collection.find(), db.collection.findAndModify(), db.collection.findOne(), db.collection.getIndexes(), db.collection.getShardDistribution(), db.collection.getShardVersion(), db.collection.group(), db.collection.insert(), db.collection.isCapped, db.collection.mapReduce(), db.collection.reIndex(), db.collection.remove(), db.collection.renameCollection(), db.collection.save(), db.collection.stats(), db.collection.storageSize(), db.collection.totalSize(), db.collection.totalIndexSize(), db.collection.update(), db.collection.validate()
    ##User Management
    db.auth(), db.createUser(), db.updateUser(), db.changeUserPassword(), db.dropAllUsers(), db.dropUser(), db.grantRolesToUser(), db.revokeRolesFromUser(), db.getUser(), db.getUsers()
    #Queries
    db.userdetails.find(); or db.userdetails.find().pretty();
    db.userdetails.find({“education”:“M.C.A.”}).pretty();
    db.userdetails.find({“education”:“M.C.A.”},{“user_id” : 1}).pretty();
    db.userdetails.find({“education”:“M.C.A.”},{“user_id” : 0}).pretty();
    collection.find().sort( {column1:1or -1 [, column2:1 or -1] });
    db.userdetails.find().skip(1).limit(2).pretty();
    db.userdetails.find({“date_of_join” : “16/10/2010”,“education”:“M.C.A.”}).sort({“profession”:-1}).pretty();
    db.userdetails.find({$query : {“date_of_join” : “16/10/2010”,“education”:“M.C.A.”}, KaTeX parse error: Expected 'EOF', got '}' at position 28: …profession":-1}}̲).pretty(); d…query : {“date_of_join” : “16/10/2010”,“education”:“M.C.A.”}, KaTeX parse error: Expected 'EOF', got '}' at position 28: …profession":-1}}̲).pretty(); d…in :[19,22]}}).pretty();
    db.userinfo.find({ “sex” : “Male” , $or : [ { “age” : 17 } , { “date_of_join” : “17/10/2009” } ] } ).pretty();
    db.testtable.find( { “interest” : null } ).pretty();
    db.testtable.find({“extra.community_name” : “MODERN MUSIC”, “extra.friends.valued_friends_id” :“harry” }).pretty();
    #MongoDB Backup and Restore
    D:\mongodb\bin>mongodump --host mypc-PC --port 27017
    D:\mongodb\bin>mongodump --out /backup_data/backup/
    D:\mongodb\bin>mongodump --collection userdetails --db myinfo
    mongodump --host [host name] --port 3017 --username [user] --password [pass] --out [backup folder path of local machine]
    D:\mongodb\bin>mongoexport --db myinfo --collection userdetails --csv --fields user_id,education,interest --out /opt/backups/userdetails.csv
    D:\mongodb\bin>mongoexport --db myinfo --collection userdetails --csv --fieldFile myfields.txt --out /backup_data/backup/userdetails.csv
    D:\mongodb\bin>mongoexport --db myinfo --collection userdetails --out /backup_data/backup/newuserdetails.json –journal
    mongoexport --host [your_domain.example.com] --port 37017 --username [name of the user] --password [your password] --collection userdetails --db myinfo --out /backup_data/backup/newuserdetails.json
    D:\mongodb\bin>mongoexport --db myinfo --collection userdetails --query “{‘interest’ : ‘MUSIC’}”;
    mongorestore dump/myinfo
    mongorestore --host [host name] --port 3017 --username [user] --password [password] [backup folder path of local machine]
    D:\mongodb\bin>mongoimport --db myinfo --collection userdetails --file newuserdetails.json
    mongoimport --host [host name] --port 37017 --username [user] --password [password] --collection userdetails --db myinfo --file \backup_data\backup\newuserdetails.json
    D:\mongodb\bin>mongoimport --db myinfo --collection userdetails --type csv --headerline --file /backup_data/backup/userdetails.csv
    D:\mongodb\bin>mongoimport --db myinfo --type csv --headerline --ignoreBlanks --file /backup_data/backup/userdetails.csv

  • 相关阅读:
    5 张图告诉你 RocketMQ 为什么不使用 Zookeeper 做注册中心
    【Redis(9)】Spring Boot整合Redis,实现分布式锁,保证分布式系统中节点操作一致性
    等保测评中web应用防火墙怎么选择?
    idea 设置文件忽略git版本控制
    ubuntu安装mysql8.0.35过程和报错处理
    报错 documentation/kbuild: is a directory. stop(Windows 内置Linux子系统WSL编译Linux内核)
    Linux基本操作思维导图(三)mv cp mkdir rmdir rm
    php+vue+Elementui大学生心理健康测评网站
    Mycat分库分表分片方式
    LocationManagerService简单分析
  • 原文地址:https://blog.csdn.net/azenlijing/article/details/125517199