mongodb入门笔记

下载:

  docker pull mongo:latest

运行:

docker run –-name mongodb -p 27017:27017 -v /data/mongo:/data/db -d mongo –auth

–name:设置容器名称
-p 端口映射,使外部通过主机ip:27017访问mongodb服务
-v 容器挂载目录,本机的/data/mongo挂载到/data/db中,作为mongodb存储目录
-d 设置容器以守护进程方式运行,docker在后台运行,不会直接把执行命令的结果输出在当前宿主主机下。
– auth 需要密码才能访问容器服务
然后进入docker:

docker exec -it mongodb /bin/bash

以admin的身份进入mongodb:

mongo admin

(也可以直接进入docker -it mongodb mongo admin)
进入以后配置用户信息:

db.createUser({user:'admin',pwd:'123456',roles:[ {role:'userAdminAnyDatabase',db:'admin'},"readWriteAnyDatabase"]});

显示:
在这里插入图片描述
则成功了。
使用账户进行认证:db.auth(“admin”,”123456”)

基本操作:
创建数据库use database_new,
这里需要注意的是user和db是绑定的,当前创建的用户只能使用当前数据库,如果要使用新的数据库,需要重新创建对应的用户并进行认证
ref

其他的基本操作可以利用python API完成,安装pymongo进行操作:具体的操作流程见连接:
pymongo基础教程

还有一些基础的操作:
在mongodb中添加字段:

## 在原数据基础上新增字段,值一样
x = mycol.update_many({}, {"$set":{"add":1}})
for x in mycol.find({}):
    print(x)

{'_id': ObjectId('6229bd99572ff0863d239dc2'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com', 'add': 1}
{'_id': ObjectId('6229bd9f572ff0863d239dc3'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com', 'add': 1}
{'_id': ObjectId('6229bd9f572ff0863d239dc4'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com', 'add': 1}
{'_id': ObjectId('6229bd9f572ff0863d239dc5'), 'name': 'Facebook', 'alexa': '10', 'url': 'https://www.facebook.com', 'add': 1}
{'_id': ObjectId('6229bd9f572ff0863d239dc6'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com', 'add': 1}
{'_id': ObjectId('6229bd9f572ff0863d239dc7'), 'name': 'Github', 'alexa': '109', 'url': 'https://www.github.com', 'add': 1}

## 在原数据上新增字段,值为array
add_dict = [{"name":"RUNOOB",
            "add_1":"t",
            "add_2":1},
           {"name":"Taobao",
           "add_1":"tmal",
           "add_2":3}]
for item in add_dict:
    mycol.update_many({"name":item["name"]}, {"$set":{"add_1":item["add_1"], "add_2":item["add_2"]}})
for x in mycol.find({}):
    print(x)

{'_id': ObjectId('6229bd99572ff0863d239dc2'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com', 'add': 1, 'add_1': 't', 'add_2': 1}
{'_id': ObjectId('6229bd9f572ff0863d239dc3'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com', 'add': 1, 'add_1': 'tmal', 'add_2': 3}
{'_id': ObjectId('6229bd9f572ff0863d239dc4'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com', 'add': 1}
{'_id': ObjectId('6229bd9f572ff0863d239dc5'), 'name': 'Facebook', 'alexa': '10', 'url': 'https://www.facebook.com', 'add': 1}
{'_id': ObjectId('6229bd9f572ff0863d239dc6'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com', 'add': 1}
{'_id': ObjectId('6229bd9f572ff0863d239dc7'), 'name': 'Github', 'alexa': '109', 'url': 'https://www.github.com', 'add': 1}

将pandas中数据转化为mongodb:

from pymongo import MongoClient
import pandas as pd

client = MongoClient("mongodb:user:pwd@ip")
db = client.admin # define the specific database
mycollection = db[collecion_name] # define colletions
raw_data = pd.read_csv("xxx")
raw_dict = raw_data.to_dict("record")
x = mycollecion.insert_many(raw_dict)
print(x.inserted_ids[:3]) # print top3 id of inserted data

读取mongodb数据转化为pandas dataframe

from pymongo import MongoClient
import pandas as pd

client = MongoClient("mongo:user:pwd@ip")
db = client.dbname
mycol = db[colletion_name]

df = pd.DataFrame(list(mycol.find({})))
df.head()

## 获取字段
def cols_find(collection):
    cols = []
    for i in range(1000):
        tmp = list(collection.find()[i])
        cols.extend(tmp)
        cols = list(set(cols))
    return cols

在这里插入图片描述

ref:
1.docker 安装
2.How can I load data from mongodb collection
3.insert new field in mongodb database
4.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值