5篇 mongodb related articles

给树莓派的ubuntu20.04系统装mongodb 4.4

Debian64位系统暂时还没有mongodb4.4的deb,编译安装也没试成功。
Ubuntu20.04 64位可以用这些命令装上。

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
 
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
 
sudo apt-get update
 
sudo apt-get install mongodb-org

装完可以启动mongod服务

systemctl start mongod
systemctl enable mongod

服务启动可能会异常,可以看下 systemctl status mongod

如报出/var/run ... 可以修改对应的服务声明 如 /lib/systemd/system/mongod.service
去掉/var

More ~

PyMongo使用密码连接Mongodb

以前的写法是这样的,

from pymongo import MongoClient
client = MongoClient(host='127.0.0.1')
db = client['dbname']
db.authenticate(user, password)

不过上面的已经废弃

warning:: Starting in MongoDB 3.6, calling :meth:authenticate
invalidates all existing cursors. It may also leave logical sessions
open on the server for up to 30 minutes until they time out.

More ~

Using mongodb service in docker-compose.yml

version: '3.1'

services:

  web:
    build: .
    restart: always
    ports:
      - 8080:80
    environment:      
      MONGODB_HOST: mongodb
      MONGODB_USER: root
      MONGODB_PWD: 123456
    depends_on:
      - mongodb

  mongodb:
    image: mongo:4.1
    restart: on-failure
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: 123456
    volumes:
      - ./data/mongo:/data/db
      - ./data/mongo-entrypoint/:/docker-entrypoint-initdb.d/

More ~

MongoDB: Query fields if not exists

官方文档的解释:
Existence Check

The { item : { $exists: false } } query matches documents that do not contain the item field:

db.inventory.find( { item : { $exists: false } } )

Studio3T里的Query查询条件可以是 {downloaded:{$exists: false}} 即可以查出那些没有这个downloaded字段的数据

More ~