使用rust操作mongodb

准备工作 使用rust进行mongodb连接需要添加依赖,在Cargo.toml中添加下面的依赖 [dependencies] mongodb="2" serde = "1" 添加serde的原因是我们建模的时候需要用。 预备知识 连接数据库 下面是我们的本地数据库连接 const CONNECT_STR:&str = "mongodb://czyt:[email protected]:27017"; 然后使用连接字符串进行数据库连接 let mut client_options = ClientOptions::parse_async(CONNECT_STR).await?; // Set the server_api field of the client_options object to Stable API version 1 let server_api = ServerApi::builder() .version(ServerApiVersion::V1) .build(); client_options.server_api = Some(server_api); // Create a new client and connect to the server let client = Client::with_options(client_options)?; // Send a ping to confirm a successful connection client.database("admin").run_command(doc! { "ping": 1 }, None)....

April 15, 2024 · 2 min · czyt

为Mongodb安装Percona Monitoring and Management

Percona Monitoring and Management (PMM) 是一种开源数据库可观察性、监控和管理工具,可与 MySQL、PostgreSQL、MongoDB 及其运行的服务器一起使用。它使您能够在一个位置查看所有数据库的节点到单个查询的性能指标。通过查询分析,您可以快速找到成本高昂且运行缓慢的查询以解决瓶颈。此外,Percona Advisors 为您提供性能、安全性和配置建议,帮助您保持数据库保持最佳性能。备份、恢复和内置开源私有 DBaaS 等警报和管理功能旨在提高 IT 团队的工作速度。 软件安装 安装环境为Ubuntu 22.04,下面的步骤参考了Percona官网的安装文档 安装服务端 本安装需要Docker环境,如果当前机器没有安装Docker,脚本会自动进行安装 使用下面命令安装 curl -fsSL https://www.percona.com/get/pmm | /bin/bash 或者 curl -fsSL https://www.percona.com/get/pmm | /bin/bash 安装完毕以后,请稍等片刻会显示server的访问信息 Gathering/downloading required components, this may take a moment Checking docker installation - installed. Starting PMM server... Created PMM Data Volume: pmm-data Created PMM Server: pmm-server Use the following command if you ever need to update your container by hand: docker run -d -p 443:443 --volumes-from pmm-data --name pmm-server --restart always percona/pmm-server:2 PMM Server has been successfully setup on this system!...

November 18, 2023 · 2 min · czyt

从Golang的开源项目中学习不同的功能实现

缘起 最近看到有些go开源项目中的代码,看到其中的功能,故整理备用。 数据结构 优先级队列 项目 https://github.com/tigrisdata/tigris // Copyright 2022-2023 Tigris Data, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied....

November 25, 2022 · 54 min · czyt

在Alibaba Cloud Linux上安装MongoDB

安装步骤 查询系统版本 执行命令lsb_release -a返回下面的内容 LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: AlibabaCloud Description: Alibaba Cloud Linux release 3 (Soaring Falcon) Release: 3 Codename: SoaringFalcon 添加yum源 创建repo文件etc/yum.repos.d/mongodb.repo并输入下面的内容,这里安装的mongodb版本为6.0,其他版本请参考官网(配置偶数版本,奇数版不适合生产使用)。 官网的配置文件如下: [mongodb-org-6.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc 使用这个配置文件是安装不了的,需要修改$releasever为相应的版本,Alibaba Cloud Linux 3修改为8 (设置一个releasever的环境变量也许也可以,没有验证。)即可。即下面的样子 [mongodb-org-6.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/6.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc 使用命令 yum -y install mongodb-org 安装即可。另外阿里云也提供了国内的镜像源,上面的配置文件可以修改为下面的内容,也是等效的。 [mongodb-org-6.0] name=MongoDB Repository baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/8/mongodb-org/6.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc 参考链接 如何在Alibaba Cloud Linux 3上安装MongoDB 5.0

August 30, 2022 · 1 min · czyt

Golang MongoDB ODM mgm使用

(本文大部分内容根据官方文档翻译而来) 环境准备 golang 1.10+ mongodb mgm 模型定义 定义 定义模型 type Book struct { // DefaultModel adds _id, created_at and updated_at fields to the Model mgm.DefaultModel `bson:",inline"` Name string `json:"name" bson:"name"` Pages int `json:"pages" bson:"pages"` } func NewBook(name string, pages int) *Book { return &Book{ Name: name, Pages: pages, } } mgm 在创建表时会自动检测Model生成的Collection名称 book:=Book{} // Print your model collection name. collName := mgm.CollName(&book) fmt.Println(collName) // 打印: books 如果要自定义生成Collection的名称。需要实现CollectionNameGetter接口。 func (model *Book) CollectionName() string { return "my_books" } // mgm return "my_books" collection coll:=mgm....

May 31, 2022 · 6 min · czyt

MongoDB操作指北

TL;DR 环境准备 mongoDB 预备知识 MongoDB常见的数据类型 数据类型 示例 说明 Null {"x" : null} Boolean {"x" : true} Number {"x" : 3.14} {"x" : 3} {"x" : NumberInt("3")} {"x" : NumberLong("3")} 默认64位浮点数,整数需要使用NumberInt和NumberLong String {"x" : "foobar"} 编码格式为UTF-8 Date {"x" : new Date()} 64位时间戳(从January 1, 1970),不存时区。通过new Date()进行调用。 Regular expression {"x" : /foobar/i} javascript 正则 Array {"x" : ["a", "b", "c"]} Embedded document {"x" : {"foo" : "bar"}} Object ID {"x" : ObjectId()} 文档12字节的ID Binary data 一个任意字节的字符串。是保存非UTF-8字符串到数据库的唯一方法。 Code {"x" : function() { /* ....

May 26, 2022 · 11 min · czyt

Rasp3b 安装MongoDB

安装 机器安装的是Manjaro,所以本文介绍的是Manjaro的树莓派3安装方式 ➜ ~ screenfetch czyt@** OS: Manjaro-ARM 22.01 Kernel: aarch64 Linux 5.15.24-1-MANJARO-ARM-RPI ##### Uptime: 21d 21h 58m ####### Packages: Unknown ##O#O## Shell: zsh 5.8.1 ####### Disk: 11G / 118G (9%) ########### CPU: BCM2835 @ 4x 1.2GHz ############# GPU: ############### RAM: 248MiB / 919MiB ################ ################# ##################### ##################### ################# 使用命令 yay -S mongodb44-bin进行安装,安装完毕后 启用服务 systemctl enable mongodb 检查服务状态 systemctl status mongodb ● mongodb.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongodb.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2022-03-23 13:11:08 CST; 11s ago Docs: https://docs....

March 23, 2022 · 2 min · czyt