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子文档的属性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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:
1 2 3 4 5 | 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内的属性。
动态连接:
1 2 3 4 5 6 7 8 9 10 11 | 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的值,可以更改连接的子文档。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步