Setup MongoDB the Docker Way
镜像地址 , 包含了很多常用操作,比如挂载卷,导出数据,设置密码等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 docker run -itd --name mymongo -v /Users/i306454/mongodb/data/db:/data/db -p 27017:27017 mongo docker exec -it mymongo mongo admin db.createUser({ user:'root' ,pwd :'root' ,roles:[ { role:'userAdminAnyDatabase' , db: 'admin' },"readWriteAnyDatabase" ]}); db.auth('root' , 'root' ) use admin db.getCollectionNames()
Issues mymongo 启动失败(之前遇到过,我记得我记录过了,怎么找不到那片文章了呢(; ̄ェ ̄)), 使用 docker logs mymongo 可以看到异常信息 “{“t”:{“$date”:”2022-01-18T08:26:55.448+00:00”},”s”:”E”, “c”:”STORAGE”, “id”:22312, “ctx”:”initandlisten”,”msg”:”Error creating journal directory”,”attr”:{“directory”:”/data/db/journal”,”error”:”boost::filesystem::create_directory: No space left on device: "/data/db/journal"“}}”,创建容器时指定挂在位置即可
MongoDB 基本操作
RDBMS
MongoDB
表
集合
行
文档
列
字段
表联合
嵌入文档
主键
主键(MongoDB 提供了 key 为 _id)
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 db use local db.local.insert({"name" :"Jack" }) use local db.dropDatabase() show dbs db.createCollection("runoob" ) show collections db.createCollection("mycol" , { capped: true , autoIndexId: true , size: 6142800, max: 10000}) db.mycal2.insert({"name" :"Jack" }) > show collections mycal2 mycol runoob db.mycal2.drop() db.collection_name.insert(doc) db.runoob.insertMany([{"key" :"name" }, {"a" :1}]) db.runoob.find() db.runoob.update({"name" :"Jack" }, {$set :{"name" :"Jerry" }}) db.col.update({"name" :"Jack" }, {$set :{"name" :"Jerry" }},{multi:true }) db.runoob.remove({"a" :1}) db.runoob.find({$or :[{"name" :"Jerry" }, {"key" :"test" }]}).pretty() db.col.find({"likes" : {$gt :50}, $or : [{"by" : "菜鸟教程" },{"title" : "MongoDB 教程" }]}).pretty() db.col.remove({}) db.col.find({likes : {$gt : 100}}) db.col.find({title: {$type :2}}) db.col.find({title: {$type :'string' }}) db.col.find({},{"title" :1,_id:0}).limit (2) db.col.find({},{"title" :1,_id:0}).limit (1).skip(1) db.col.find({},{"title" :1,_id:0}).sort({"likes" :-1}) db.col.createIndex({"title" :1}) db.values.createIndex({open: 1, close: 1}, {background: true }) db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION) db.col.count() db.col.count({'age' :24})
集成 pymongo 安装 lib pip install pymongo
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 from pymongo import MongoClientclient = MongoClient() client.list_database_names() collection = client["ttt" ] collection.list_collection_names() mydoc = collection["sites" ] mydict = { "name" : "RUNOOB" , "alexa" : "10000" , "url" : "https://www.runoob.com" } mydoc.insert_one(mydict) mydoc.insert_many(mylist) mydoc.find_one() for sub in mydoc.find(): print(sub) for x in mydoc.find({},{ "_id" : 0 , "name" : 1 , "alexa" : 1 }): print(x) mydoc.find({ "name" : "RUNOOB" }) mydoc.find({ "name" : { "$gt" : "H" } }) mydoc.find({ "name" : { "$regex" : "^R" } }) mydoc.find().limit(3 ) myquery = { "alexa" : "10000" } newvalues = { "$set" : { "alexa" : "12345" } } mydoc.update_one(myquery, newvalues) mydoc.find().sort("alexa" ) mydoc.find().sort("alexa" , -1 ) myquery = { "name" : "Taobao" } mydoc.delete_one(myquery) myquery = { "name" : {"$regex" : "^F" } } mydoc.delete_many(myquery) mydoc.delete_many({}) mydoc.drop()