最近经常用到mongodb,现把常用命令汇总如下:
1、创建集合
db.createCollection("modb_test")
2、复制集合(同库复制)
1 db.modb_test.find().forEach(function(x){db.modb_test2.insert(x)})
3、删除集合
1 db.modb_test2.drop()
4、查看一条
1 db.modb_test.findOne() 2 3 { 4 "_id" : "5cda9deeac91bb0001e3d582", 5 "username" : "igcptest103", 6 "phone" : "13820555567", 7 "create_time" : "2019-05-14 18:52:30", 8 "user_id" : "9b840357-6dc8-48c6-b2e1-081466bd93a8", 9 "realname" : "igcptest103", 10 "is_delete" : 0, 11 "site_info" : [ 12 { 13 "pro_list" : [ 14 { 15 "role_list" : [ 16 "5cda9e25ac91bb0001e3d584" 17 ], 18 "pro_id" : "9cced61a0726b762781b199e" 19 }, 20 { 21 "role_list" : [ 22 "5cda9e25ac91bb0001e3d584" 23 ], 24 "pro_id" : "76faada2-3381-450c-bca4-4527b44a5b76" 25 }, 26 { 27 "role_list" : [ 28 "5cda9e25ac91bb0001e3d584" 29 ], 30 "pro_id" : "55439bed-de5a-4539-bee8-33c7ee97a7cc" 31 } 32 ], 33 "sys_role_id" : "5cda9e1aac91bb0001e3d583", 34 "site_id" : "5cc02b9e0726b7066e0d420b", 35 "company_type" : null, 36 "global_project_role_id" : "5cda9e36ac91bb0001e3d585", 37 "belong" : "2", 38 "company_id" : "5c67e745-618e-4e76-9202-b692c345c940", 39 "office_id" : "", 40 "is_valid" : 0, 41 "max_pro_num" : 33, 42 "join_pro_num" : 0 43 } 44 ], 45 "email" : "[email protected]" 46 }
5、查看多条
db.modb_test.find().pretty()
6、向_id为5cda9deeac91bb0001e3d582的site_info的list中插入一条数据
1 db.modb_test.update({"_id":"5cda9deeac91bb0001e3d582"},{$push:{"site_info":{"site_id":"casdfweradc2238db9adbdcd"}}}) 2 3 查询结果 4 db.modb_test.find({"_id":"5cda9deeac91bb0001e3d582"}).pretty() 5 6 { 7 "_id" : "5cda9deeac91bb0001e3d582", 8 "username" : "igcptest103", 9 "phone" : "13820555567", 10 "create_time" : "2019-05-14 18:52:30", 11 "user_id" : "9b840357-6dc8-48c6-b2e1-081466bd93a8", 12 "realname" : "igcptest103", 13 "is_delete" : 0, 14 "site_info" : [ 15 { 16 "pro_list" : [ 17 { 18 "role_list" : [ 19 "5cda9e25ac91bb0001e3d584" 20 ], 21 "pro_id" : "9cced61a0726b762781b199e" 22 }, 23 { 24 "role_list" : [ 25 "5cda9e25ac91bb0001e3d584" 26 ], 27 "pro_id" : "76faada2-3381-450c-bca4-4527b44a5b76" 28 }, 29 { 30 "role_list" : [ 31 "5cda9e25ac91bb0001e3d584" 32 ], 33 "pro_id" : "55439bed-de5a-4539-bee8-33c7ee97a7cc" 34 } 35 ], 36 "sys_role_id" : "5cda9e1aac91bb0001e3d583", 37 "site_id" : "5cc02b9e0726b7066e0d420b", 38 "company_type" : null, 39 "global_project_role_id" : "5cda9e36ac91bb0001e3d585", 40 "belong" : "2", 41 "company_id" : "5c67e745-618e-4e76-9202-b692c345c940", 42 "office_id" : "", 43 "is_valid" : 0, 44 "max_pro_num" : 33, 45 "join_pro_num" : 0 46 }, 47 { 48 "site_id" : "casdfweradc2238db9adbdcd" 49 } 50 ], 51 "email" : "[email protected]" 52 }
7、删除site_info中site_id为casdfweradc2238db9adbdcd的数据
1 db.modb_test.update({"_id":"5cda9deeac91bb0001e3d582"},{$pull:{"site_info":{"site_id":"casdfweradc2238db9adbdcd"}}})
8、只列出site_info中site_id为5cc02b9e0726b7066e0d420b的节点数据
1 db.modb_test.find({"_id":"5cda9deeac91bb0001e3d582"},{"site_info":{$elemMatch:{"site_id":"5cc02b9e0726b7066e0d420b"}}}) 2 3 查询结果 4 { 5 "_id" : "5cda9deeac91bb0001e3d582", 6 "site_info" : [ 7 { 8 "pro_list" : [ 9 { 10 "role_list" : [ 11 "5cda9e25ac91bb0001e3d584" 12 ], 13 "pro_id" : "9cced61a0726b762781b199e" 14 }, 15 { 16 "role_list" : [ 17 "5cda9e25ac91bb0001e3d584" 18 ], 19 "pro_id" : "76faada2-3381-450c-bca4-4527b44a5b76" 20 }, 21 { 22 "role_list" : [ 23 "5cda9e25ac91bb0001e3d584" 24 ], 25 "pro_id" : "55439bed-de5a-4539-bee8-33c7ee97a7cc" 26 } 27 ], 28 "sys_role_id" : "5cda9e1aac91bb0001e3d583", 29 "site_id" : "5cc02b9e0726b7066e0d420b", 30 "company_type" : null, 31 "global_project_role_id" : "5cda9e36ac91bb0001e3d585", 32 "belong" : "2", 33 "company_id" : "5c67e745-618e-4e76-9202-b692c345c940", 34 "office_id" : "", 35 "is_valid" : 0, 36 "max_pro_num" : 33, 37 "join_pro_num" : 0 38 } 39 ] 40 }
9、内嵌节点操作,用点做连接操作符
1 db.modb_test.find("site_info.pro_list.pro_id":"9cced61a0726b762781b199e")
10、按条件查询对应数据,然后往site_info中的pro_list中插入数据
1 db.modb_test.update({"_id":"5cda9deeac91bb0001e3d582","site_info.site_id":"5cc02b9e0726b7066e0d420b"},{$push:{"site_info.$.pro_list":{"role_list":[],"pro_id":"12387123261a0726b762781b199e"}}}) 2 3 插入后的结果 4 { 5 "_id" : "5cda9deeac91bb0001e3d582", 6 "username" : "igcptest103", 7 "phone" : "13820555567", 8 "create_time" : "2019-05-14 18:52:30", 9 "user_id" : "9b840357-6dc8-48c6-b2e1-081466bd93a8", 10 "realname" : "igcptest103", 11 "is_delete" : 0, 12 "site_info" : [ 13 { 14 "pro_list" : [ 15 { 16 "role_list" : [ 17 "5cda9e25ac91bb0001e3d584" 18 ], 19 "pro_id" : "9cced61a0726b762781b199e" 20 }, 21 { 22 "role_list" : [ 23 "5cda9e25ac91bb0001e3d584" 24 ], 25 "pro_id" : "76faada2-3381-450c-bca4-4527b44a5b76" 26 }, 27 { 28 "role_list" : [ 29 "5cda9e25ac91bb0001e3d584" 30 ], 31 "pro_id" : "55439bed-de5a-4539-bee8-33c7ee97a7cc" 32 }, 33 { 34 "role_list" : [ ], 35 "pro_id" : "12387123261a0726b762781b199e" 36 } 37 ], 38 "sys_role_id" : "5cda9e1aac91bb0001e3d583", 39 "site_id" : "5cc02b9e0726b7066e0d420b", 40 "company_type" : null, 41 "global_project_role_id" : "5cda9e36ac91bb0001e3d585", 42 "belong" : "2", 43 "company_id" : "5c67e745-618e-4e76-9202-b692c345c940", 44 "office_id" : "", 45 "is_valid" : 0, 46 "max_pro_num" : 33, 47 "join_pro_num" : 0 48 } 49 ], 50 "email" : "[email protected]" 51 }