zoukankan      html  css  js  c++  java
  • mongodb笔记(一) 分片 &&文档连接

    版本:mongodb3.4 ;

    分片:

      工作顺序:router=>config=>shards

     一,配置config:

       3.4中config必须为replSet。下面配置两个config。

       mongod --configsvr -dbpath= ..   -logpath=..   replSet=myConfig -port=3001 --fork;

         mongod --configsvr -dbpath=..    -logpaht=..  replSet=myConfig  -port=3002 --fork;

        进入config,mongo --port 3001,配置副本集:

       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);

        官方建议三个,我们这里只有两个。host,不能为127.0.0.1,或者localhost。所以我配置为局域网的地址了。

      二,配置shard:

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

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

      生存环境下建议shard为replSet,这里姑且就为mongod吧

     三,配置router:

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

      将config副本集写进去

     四,定义shard:

      进入mongos :

             mongo -host=155.155.1.104  -port=3000

      use admin;

      添加分片:

          sh.addShard( '155.155.1.104:4001');   sh.addShard('155.155.1.104:4002');

      激活一个db:

          sh.enableSharding('test');

      为该db中的collection添加规则:

          sh.shardCollection('test.collection1',{_id:'hashed'})

     

    文档连接:

    文档引用

       官方使用的方法,将一个document 的field指向另一个document的_id。

    树形结构:

      子定义一个parent。父定义一个children。将子父的一个filed放入其中。可以建立索引。更遥远的可以定义ancestor。

    树形路径:

      定义document的一个filed为string型  ',path,path1,path2'。根据树形结构,写放路径。

    二叉树路径:

      每个document,拥有一个left,right为number型。根据left,right和二叉树结构,可以遍历其子document。

    数据引用:

      将document的一个filed定义为DBRef,其结构如下:

            creator:{

               $ref:  //指向collection;                        =》namespace  //在node.js中获得DBRef时,所对应的属性。

              $db:  //指向db;                                =》db

              $id: //指向collection.document._id; =》 oid

            }

    mongodb does not support joins。

    介绍下mongoose population:

      连接两个文档,并通过父文档find子文档的属性:

    var parentSchema=new Schema({
          name:String,
          children:{type:Schema.Types.ObjectId, ref:'children'}
        })    
    var childSchema=new Schema({
          name:String
    })
    
    // 在父schema中定义children,指向子document的_id,使用ref指向该collection。
    //下面是创建这两个document,分别在parent和children这两个collection中。
    
    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();
    })
    
    //这样就将两个document分别写入其各自的collection 中了。
    //下面是使用他们;
    
    parentModel.findOne({name:'A'}).populate('children')  //populate 中的值为parentSchema的children属性.
    .exec((err,doc)=>{
        console.log(doc.children.name);   //show 'B'
    })
    

      update

    parentModel.findOne({name:'A'}).populate('children')
    .exec((err,doc)=>{
         doc.children= ...;              //重新指定一个document。可以是document._id。也可以直接传递document
         doc.save(callback);             
    })
    

      很遗憾的是不能通过doc.children.name='';doc.save();这种方式来更改children document内的属性。

          动态连接:

    var parentSchem=new Schema({
        name:String,
       children:[ { 
                 kind:String,
                 item:{type:ObjectId ,     refPath: 'children.kind '} } ]
    })    
    
    //refPath指向children.kind。通过kind的值,来定义类似之前ref的值;
    //使用:
    
    parentMode.findOne({name:'A'}).populate('children.item').exec()
    

      ref的值是不能直接更改的。所以通过更改kind的值,可以更改连接的子文档。

          

     
  • 相关阅读:
    【LeetCode 15】三数之和
    【LeetCode 14】最长公共前缀
    【LeetCode 13】罗马数字转整数
    【LeetCode 12】整数转罗马数字
    【LeetCode 11】盛最多水的容器
    【LeetCode 10】正则表达式匹配
    【LeetCode 9】回文数
    【LeetCode 8】字符串转换整数 (atoi)
    【LeetCode 7】整数反转
    【LeetCode 6】Z 字形变换
  • 原文地址:https://www.cnblogs.com/ztwBlog/p/6275670.html
Copyright © 2011-2022 走看看