0
点赞
收藏
分享

微信扫一扫

Node Mongdb初试 -向coll添加验证器-远程访问-node连接


文章目录

  • ​​环境: ubuntu 20.04​​
  • ​​远程连接 bindIp:0.0.0.0​​
  • ​​开启访问控制 security.authorization=enabled​​
  • ​​创建一个admin​​
  • ​​创建一个数据库​​
  • ​​向数据库中插入数据​​
  • ​​插入一条​​
  • ​​插入多条​​
  • ​​NODE 连接 MONGODB​​
  • ​​node-CURD-by-mongo​​
  • ​​模式验证​​
  • ​​创建集合时添加验证器 `validator`​​
  • ​​向已有的集合添加验证器​​

环境: ubuntu 20.04

配置文件: /etc/mongod.conf

远程连接 bindIp:0.0.0.0

开启访问控制 security.authorization=enabled

security:
authorization: enabled

​​开启访问控制文档​​

创建一个admin

use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "passwordPrompt",
// pwd: passwordPrompt(),
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)

passwordPrompt的作用是自己键盘输入密码不用明文写
创建一个 root

db.createUser(
{
user:"root",
pwd:"pwd",
roles:["root"]
}
)

创建一个数据库

​use newDB​​ 没有就创建 有就切换

向数据库中插入数据

插入一条

use newDB // 指定哪个数据库中执行 db表示这个数据库
db.collName.insertOne({name:"hello"})

格式​​[数据库名].[集合名].insertOne({key:value})​

集合相当于sql中的表
文档相当于sql中的元组

插入多条

db.collName.insertMany([
{nickname:'b'},
{nickname:'c'}
])

NODE 连接 MONGODB

const options = {
username: "toolsBoxAdmin",
password: "xxxx",
host: "159.75.22.82",
port: "27017",
db_name: "toolsBox",
};
const dbUrl = `mongodb://${options.username}:${options.password}@${options.host}:${options.port}/${options.db_name}`;

const MongoClient = require("mongodb").MongoClient;

MongoClient.connect(dbUrl, function (err, db) {
if (err) throw err;
console.log("ok!");
db.close();
});

OR
文档​​​https://www.npmjs.com/package/mongodb​​

const { MongoClient } = require("mongodb");

const options = {
host: "159.75.22.82",
port: "27017",
db_name: "toolsBox",
};

const MongoClientConfig = {
useUnifiedTopology: true,
auth: {
user: "xxxxxx",
password: "xxxxx",
},
};

const url = `mongodb://${options.host}:${options.port}/${options.db_name}`;

const client = new MongoClient(url, MongoClientConfig);

client.connect(async (err, client) => {
if (err) return;
console.log("OK!👏");
const result = await client
.db() //使用传入的库名 也可传参别的数据库名
.collection("userProfile") // 选择表/集合
.find() //全部数据
.toArray(); //Promise
// .findOne({ nickname: "a" })
console.log("result:", result);
});

查找
nickname等于"c"的全部数据

.find({ nickname: { $eq: "c" } }

db.todoList.insert({"context":"123","status": false,"reminderTime":ISODate("2021-06-15T00:00:00Z")})

Node Mongdb初试 -向coll添加验证器-远程访问-node连接_mongodb


md告诉验证错误 不告诉哪个错了 这让写description干🐔

node-CURD-by-mongo

node 使用mongo

const result = await client.db().collection("todoList").insert({
context: "sleep ",
// reminderTime: ISODate("2021-06-14T20:00:00Z"),
reminderTime: new Date(),
});

mongo shell

db.todoList.insert({context:"shop ",reminderTime: ISODate("2021-06-14T20:00:00Z")})

​​模式验证​​

创建集合时添加验证器 ​​validator​

db.createCollection("todoList", {
validator: { $jsonSchema: {
bsonType: "object",
required: [ "content","reminderTime" ],
properties: {
content: {
bsonType: "string",
description: "must be a string and is required"
},
reminderTime: {
bsonType : "date",
description: "must be a Date and gt now"
},
}
} },
validationAction: "error"
} )

向已有的集合添加验证器

​​link​​

db.contacts.insert([
{ "_id": 1, "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" },
{ "_id": 2, "name": "Ivan", "city": "Vancouver" }
])

发出以下命令以向contacts 集合添加验证器:

db.runCommand( {
collMod: "contacts",
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone", "name" ],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
name: {
bsonType: "string",
description: "must be a string and is required"
}
}
} },
validationLevel: "moderate"
} )


举报

相关推荐

0 条评论