MongoDB notes (i) Shard && Document Connection

Source: Internet
Author: User
Tags findone

Version: mongodb3.4;

Sharding:

Working order: Router=>config=>shards

One, configure config:

The 3.4 config must be replset. Configure two config below.

Mongod--configsvr-dbpath=.   -logpath=. replset=myconfig-port=3001--fork;

Mongod--configsvr-dbpath=.  -logpaht=. Replset=myconfig-port=3002--fork;

Enter Config,mongo--port 3001, configure the replica set:

var config={

_id: ' Myconfig ',

Configsvr:true,

memeber:[{_id:0,host:155.155.1.104:3001}, {_id:0,host:155.155.1.104:3002}]

}

Rs.initiate (config);

The official recommendation is three, we have only two here. Host, cannot be 127.0.0.1, or localhost. So I configured the address of the LAN.

Two, configure Shard:

Mongod--shardsvr dbpath=. -logpath=. -port=4001-nojournal--fork

Mongod--shardsvr dbpath=. -logpath=. -port=4002-nojournal--fork

Living Environment Recommended Shard for Replset, here for Mongod Bar

Three, configure router:

mongos-configdb=myconfig/155.155.1.104:3001,155.155.1.104:3002-port=3000

Write the config copy set in

Four, define Shard:

  Enter MONGOs:

mongo-host=155.155.1.104-port=3000

  Use admin;

  To add a shard:

Sh.addshard (' 155.155.1.104:4001 '); Sh.addshard (' 155.155.1.104:4002 ');

  Activate a DB:

Sh.enablesharding (' Test ');

  Add a rule for the collection in the DB:

Sh.shardcollection (' Test.collection1 ', {_id: ' hashed '})

Document Connection:

Document reference

The official method of using a document field points to the _id of another document.

Tree-shaped structure:

The child defines a parent. The parent defines a children. Place a filed of the child parent into it. Indexes can be built. More distant can define the ancestor.

Tree-shaped path:

Defines a filed of document as String ', Path,path1,path2 '. Writes the path according to the tree structure.

Binary Tree path:

Each document has a left,right of type number. Depending on the left,right and two fork tree structure, the child document can be traversed.

Data references:

Define a filed of document as Dbref, which is structured as follows:

creator:{

$ref://point to collection; = "namespace//when DBREF is obtained in node. js, the corresponding property.

$DB://point to DB; = "db"

$id://point to collection.document._id; = "OID"

}

MongoDB does not a support joins.

Introduction to the next Mongoose population:

Connect two documents and find the properties of a subdocument through the parent document:

var parentschema=new Schema ({      name:string,      children:{type:schema.types.objectid, ref: ' Children '}    }) The    var childschema=new schema ({      name:string})//Defines the children in the parent schema, points to the _id of the child document, and points to the collection using Ref. The following is the creation of these two document (s) in the two collection of the parent and children respectively. var Parentmodel=mongoose.model (' Parent ', Parentschema), Var childrenmodel=mongoose.model (' Children ', childschema); var childone=new childrenmodel ({name: ' B '}), Childone.save (val=>{(    new Parentmodel ({           name: ' A ',           children:childone._id     }). Save ();}) In this way, two document is written to its respective collection. Below is the use of them; Parentmodel.findone ({name: ' A '}). Populate (' Children ')  //populate the value in Parentschema children property: EXEC ((err,doc) =>{    console.log (doc.children.name);   Show ' B '})

  Update:

Parentmodel.findone ({name: ' A '}). Populate (' Children '). EXEC ((err,doc) =>{     doc.children= ...;              Re-specify a document. It could be document._id. Document Doc.save (callback) can also be passed directly     ;             })

Unfortunately, it is not possible to change the properties in children document by Doc.children.name= ';d oc.save ().

Dynamic Connection:

var parentschem=new Schema ({    name:string,   children:[{              kind:string,             Item:{type:objectid,     Refpath: ' Children.kind '}]})    //refpath point to Children.kind. A value similar to the previous ref is defined by the value of kind;//used: Parentmode.findone ({name: ' A '}). Populate (' Children.item '). EXEC ()

The value of ref cannot be changed directly. So by changing the value of the kind, you can change the attached subdocument.

MongoDB notes (i) Shard && Document Connection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.