Skip to content

Commit

Permalink
feat: mongodb code
Browse files Browse the repository at this point in the history
  • Loading branch information
awayjin committed Apr 2, 2020
1 parent 569add1 commit 6775bc8
Show file tree
Hide file tree
Showing 98 changed files with 2,228 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
git branch -r

git checkout -b 本地分支名x origin/远程分支名x


### Node.js 中 console 彩色
console.log('\033[42;30m DONE \033[40;32m C \033[0m')
40 changes: 29 additions & 11 deletions js/relearning/08-objects-classify.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,31 @@
</head>
<body>

<a href="https://time.geekbang.org/column/article/80011"></a>
js 对象分类(https://time.geekbang.org/column/article/80011)


<script>
class Exception extends Error {
constructor (msg = 'why wrong', errorCode = '10001', code = '400') {
super()
this.msg = msg
this.errorCode = errorCode
this.code = code
}
}

try {
a
} catch (e) {
let e1 = new Exception()
console.log('e1:', e1)
throw e1
}
</script>


<script>
var set = new Set();
var objects = [
eval,
Expand Down Expand Up @@ -61,20 +84,15 @@
objects.forEach(o => set.add(o));

console.log('objects start:' + objects.length)

for(var i = 0; i < objects.length; i++) {
var o = objects[i]
for(var p of Object.getOwnPropertyNames(o)) {
var d = Object.getOwnPropertyDescriptor(o, p)
if( (d.value !== null && typeof d.value === "object") || (typeof d.value === "function"))
console.log('----d----:' + 'bool:' + typeof d.value)
console.log(d)
console.log('o:')
console.log(o)
// debugger
if(!set.has(d.value))
if(!set.has(d.value))
set.add(d.value), objects.push(d.value);
if( d.get )
// debugger
if(!set.has(d.get))
set.add(d.get), objects.push(d.get);
if( d.set )
Expand All @@ -94,9 +112,9 @@
for (var value of Object.getOwnPropertyNames(arr)) {

var d = Object.getOwnPropertyDescriptor(arr, value)
console.log('------1...-------value:' + value + '----d.value:' + d.value + ', d.get:' + d.get)
console.log(d)
console.log(' \n')
// console.log('------1...-------value:' + value + '----d.value:' + d.value + ', d.get:' + d.get)
// console.log(d)
// console.log(' \n')
if (d.get) break

// console.log(p)
Expand All @@ -108,4 +126,4 @@
</script>

</body>
</html>
</html>
53 changes: 53 additions & 0 deletions nodejs/demo/inquirer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const inquirer = require('inquirer')

const promptList = [
{
type: 'input',
name: 'userName',
message: 'set a user name:',
default: 'test_user',
// validate: function (val) {
// if (val.length === 3) {
// return val
// }
// return '请输入3位数字.'
// }
},
{
type: 'input',
message: '请输入手机号:',
name: 'phone',
validate: function(val) {
if(val.match(/\d{3}/g)) { // 校验位数
var done = this.async();
done(null, true)
return val;
}
return "请输入3位数字";
}
},
{
type: 'confirm',
name: 'test',
message: 'Are you handsome?',
default: true
}
]

inquirer
.prompt(promptList)
.then(answers => {
console.log('answers:', answers)
console.log('%c answers:', answers, 'font-size: 14px; color: red;')
})

// const list = [{
// type: 'confirm',
// name: 'test',
// message: 'Are you handsome?',
// default: true
// }]
// inquirer.prompt(list).then((answers) => {
// console.log('结果为:');
// console.log(answers);
// })
13 changes: 13 additions & 0 deletions nodejs/demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "demo",
"version": "1.0.0",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"inquirer": "^7.1.0"
}
}
2 changes: 2 additions & 0 deletions nodejs/demo/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## inquirer.js —— 一个用户与命令行交互的工具
https://blog.csdn.net/qq_26733915/article/details/80461257
4 changes: 2 additions & 2 deletions nodejs/july/isLand/middlewares/exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const catchError = async (ctx, next) => {

const isHttpException = e instanceof HttpException
const isDev = config.environment === 'dev'
// 开发环境,抛出异常
// 开发环境,未知异常被抛出
if (isDev && !isHttpException) {
throw e
}
Expand All @@ -34,4 +34,4 @@ const catchError = async (ctx, next) => {
}
}

module.exports = catchError
module.exports = catchError
2 changes: 2 additions & 0 deletions sql/mongodb/01-02.production.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,5 @@ MongoDB 在保留 JSON 基本键 / 值对特性的基础上,添加了其他一
MongoDB 中存储的文档必须有一个 "_id" 键。这个键的值可以是任何类型的,默认是个 ObjectId 对象。

## 2.7 使用MongoDB shell

help()
2 changes: 2 additions & 0 deletions sql/mongodb/MDBAuthedGuideSrc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Created by .ignore support plugin (hsz.mobi)
.idea/*
2 changes: 2 additions & 0 deletions sql/mongodb/MDBAuthedGuideSrc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# MDBAuthedGuideSrc
《MongoDB权威指南(第二版)》源码整理
1 change: 1 addition & 0 deletions sql/mongodb/MDBAuthedGuideSrc/ch01/info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### 第1章 MongoDB简介
38 changes: 38 additions & 0 deletions sql/mongodb/MDBAuthedGuideSrc/ch02/01--crud/crud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/***
* CRUD 操作
***/

/***
* 创建文档
***/
post=//
{
"title":"My Blog Post",
"content":"Here is my blog post.",
"date":ISODate("2012-08-24T21:12:09.982Z")
}

//插入(创建)新文档
db.blog.insert(post);

// 查询集合 blog
db.blog.find();

/***
*查询一条记录
***/
db.blog.findOne();

/***
*更新文档
***/
// 增加 comments 评论功能
post.comments=[];
db.blog.update({title:"My Blog Post"},post);
db.blog.findOne();

/***
*删除文档
***/
db.blog.remove({title:"My Blog Post"});

17 changes: 17 additions & 0 deletions sql/mongodb/MDBAuthedGuideSrc/ch02/02--myshell/defineConnectTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 自定义连接函数脚本
var connectTo=function(port,dbname)//
{
if(!port)//
{
port=27017;//默认端口
}

if(!dbname) //
{
dbname="test";//默认连接的数据库
}

// db 指向目标数据库
db=connect("localhost:"+post+"/"+dbname);
return db;
}
51 changes: 51 additions & 0 deletions sql/mongodb/MDBAuthedGuideSrc/ch02/02--myshell/mongorc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 创建 mongorc.js 执行常用脚本
var compliment=["attractive","intelligent","like Batman"];
var index=Math.floor(Math.random()*3);

print("Hello,you are looking paricularly "+compliment[index]+" today");

/***
*禁止某些危险函数
***/
// var no=function()//
// {
// print("Not on my watch");
// }
//
// //禁止删除数据库
// db.dropDatabase=DB.prototype.dropDatabase=no;
//
// //禁止删除集合
// DBCollection.prototype.drop=no;
//
// //禁止删除索引
// DBCollection.prototype.dropIndex=no;

// 定制 shell 提示 —— 显示当前时间
// prompt=function()//
// {
// return (new Date())+"> ";
// }

// 定制 shell 提示 —— 显示当前使用的数据库
prompt=function()//
{
if (typeof db=="undefined") //
{
return "(nodb)> ";
}

// 检查最后的数据库操作
try//
{
db.runCommand({getLastError:1});
}catch(e)//
{
print(e);
}

return db+">";
}


EDITOR="D:/Sublime Text 3/sublime_text";
52 changes: 52 additions & 0 deletions sql/mongodb/MDBAuthedGuideSrc/ch02/02--myshell/myshell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/***
*定义 shell 功能
***/
// 连接到指定服务器的指定数据库
mongo 127.0.0.1:27017/test

db;

// 启动时不连接任何数据库
mongo --nodb

// 启动后转链接其他数据库
conn=new Mongo("127.0.0.1:27017");
db=conn.getDB("blog");

// 查看帮助
help

// 查看数据库级别帮助
db.help();

// 查看集合级别帮助
db.blog.help();

//查看函数的用法
db.blog.update;

// 加载脚本 defineConnectTo.js
typeof connectTo;

load('defineConnectTo.js');

typeof connectTo;

// 查看当前工作目录
run("pwd");

// 启动时禁止加载 .mongorc.js
mongo --norc


// 调用编辑器编辑复杂变量
var wap=db.blog.findOne();

edit wap;

/***
*访问特殊集合
***/
db.version;// 仅仅访问了内部的方法

db.getCollection("version"); //获取到集合
3 changes: 3 additions & 0 deletions sql/mongodb/MDBAuthedGuideSrc/ch02/02--myshell/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tip="我是测试脚本";

print(tip);
1 change: 1 addition & 0 deletions sql/mongodb/MDBAuthedGuideSrc/ch02/info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### 第2章 MongoDB基础知识
32 changes: 32 additions & 0 deletions sql/mongodb/MDBAuthedGuideSrc/ch03/01-insert/ins-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/***
*插入测试
***/
/***
* 简单插入
***/
db.foo.insert({"bar":"baz"});

db.foo.findOne();

/***
* 批量插入
***/
db.foo.insert([
{"_id":0},
{"_id":1},
{"_id":2}
]);

db.foo.find();

// 插入错误时候,前面会插入成功,后面会全部不插入
db.foo.insert([
{"_id":6},
{"_id":7},
{"_id":2},
{"_id":8}
]);

db.foo.find();


Loading

0 comments on commit 6775bc8

Please sign in to comment.