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