Gowhich

Durban's Blog

最近使用mongodb进行统计查询,遇到了下面的错误提示

mongo 错误提示:OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit

也许你遇到了跟我一样的问题,不知道如何解决

其原因是

Mongodb的sort操作是把数据拿到内存中再进行排序的,为了节约内存,默认给sort操作限制了最大内存为32Mb,当数据量越来越大直到超过32Mb的时候就自然抛出异常了.值得注意是,如果你使用 skip 和 limit 联合使用实际也会产生排序.

下面是两种方式,仅供参考

方法1:增加缓存

1
2
3
4
//查询值
db.runCommand({ "getParameter" : 1, "internalQueryExecMaxBlockingSortBytes" : 1 } )
//设置新值
db.adminCommand({"setParameter":1, "internalQueryExecMaxBlockingSortBytes":335544320})

方法2:增加索引

根据业务对排序的key进行建立索引

我采用的方式是第二种方法

参考文章,点击这里

TypeScript Challenge - 实现 Omit<T, K>

题目


实现 Omit<T, K>

不使用 Omit 实现 TypeScript 的 Omit<T, K> 范型。

Omit 会创建一个省略 K 中字段的 T 对象。

例如:

1
2
3
4
5
6
7
8
9
10
11
interface Todo {
title: string
description: string
completed: boolean
}

type TodoPreview = MyOmit<Todo, 'description' | 'title'>

const todo: TodoPreview = {
completed: false,
}

测试用例


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { Equal, Expect } from '@type-challenges/utils'

type cases = [
Expect<Equal<Expected1, MyOmit<Todo, 'description'>>>,
Expect<Equal<Expected2, MyOmit<Todo, 'description' | 'completed'>>>
]

interface Todo {
title: string
description: string
completed: boolean
}

interface Expected1 {
title: string
completed: boolean
}

interface Expected2 {
title: string
}

答案

1
2
3
type MyOmit<T, K> = {
[P in Exclude<keyof T,K>]: T[P]
}

TypesScript Challenge 学习记录 - 获取函数返回类型

题目


不使用 ReturnType 实现 TypeScript 的 ReturnType<T> 范型。

例如:

1
2
3
4
5
6
7
8
const fn = (v: boolean) => {
if (v)
return 1
else
return 2
}

type a = MyReturnType<typeof fn> // 应推导出 "1 | 2"

测试用例


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { Equal, Expect } from '@type-challenges/utils'

type cases = [
Expect<Equal<string, MyReturnType<() => string>>>,
Expect<Equal<123, MyReturnType<() => 123>>>,
Expect<Equal<ComplexObject, MyReturnType<() => ComplexObject>>>,
Expect<Equal<Promise<boolean>, MyReturnType<() => Promise<boolean>>>>,
Expect<Equal<() => 'foo', MyReturnType<() => () => 'foo'>>>,
Expect<Equal<1 | 2, MyReturnType<typeof fn>>>,
Expect<Equal<1 | 2, MyReturnType<typeof fn1>>>,
]

type ComplexObject = {
a: [12, 'foo']
bar: 'hello'
prev(): number
}

const fn = (v: boolean) => v ? 1 : 2
const fn1 = (v: boolean, w: any) => v ? 1 : 2

答案


1
type MyReturnType<T> = T extends (...args: []) => infer P ? P : never

今天在执行brew doctor的时候出现如下错误:

1
2
$ brew doctor
Error: Cask 'java' is unreadable: undefined method `undent' for #<String:0x00007f86a64ab968>

原因为某次更新之后,配置文件增加了某些不必要的字段。更要命的是,不能执行卸载命令来删除出问题的安装包。解决方法为删除这个字段。

方案如下:

1
$ find "$(brew --prefix)/Caskroom/"java'/.metadata' -type f -name '*.rb' | xargs grep 'EOS.undent' --files-with-matches | xargs sed -i '' 's/EOS.undent/EOS/'

或者下面(我的是java)

1
$ find "$(brew --prefix)/Caskroom/"java7'/.metadata' -type f -name '*.rb' | xargs grep 'EOS.undent' --files-with-matches | xargs sed -i '' 's/EOS.undent/EOS/'

操作完之后在更新下brew

下面的操作是基于java(因为我的是java)

1
$ brew uninstall java 
1
$ brew cleanup
1
$ brew update
1
$ brew upgrade

参考文章地址: https://www.mobibrw.com/2020/26381

git version 2.28.0

出现问题时执行的命令

1
git pull origin master

warning提示如下

warning: 不建议在没有为偏离分支指定合并策略时执行 pull 操作。 您可以在执行下一次
pull 操作之前执行下面一条命令来抑制本消息:

git config pull.rebase false # 合并(缺省策略)
git config pull.rebase true # 变基
git config pull.ff only # 仅快进

您可以将 “git config” 替换为 “git config --global“ 以便为所有仓库设置
缺省的配置项。您也可以在每次执行 pull 命令时添加 –rebase、–no-rebase,
或者 –ff-only 参数覆盖缺省设置。

Providers是什么,看下官网的解释

Providers are a fundamental concept in Nest. Many of the basic Nest classes may be treated as a provider – services, repositories, factories, helpers, and so on. The main idea of a provider is that it can inject dependencies; this means objects can create various relationships with each other, and the function of “wiring up” instances of objects can largely be delegated to the Nest runtime system. A provider is simply a class annotated with an @Injectable() decorator.

Services

通过命令行创建 Services

1
nest g service cats

执行后结果类似如下

1
2
3
4
$ nest g service cats
CREATE src/cats/cats.service.spec.ts (446 bytes)
CREATE src/cats/cats.service.ts (88 bytes)
UPDATE src/app.module.ts (544 bytes)

创建完src/cats/cats.service.ts代码如下

1
2
3
4
import { Injectable } from '@nestjs/common';

@Injectable()
export class CatsService {}

修改下,修改前提我们添加一个interface

如果创建interface,命令行命令如下

1
2
$ nest g interface cat
CREATE src/cat.interface.ts (24 bytes)

创建完src/cat.interface.ts代码如下

1
export interface Cat {}

修改后src/cats/cats.service.ts的代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { Injectable } from '@nestjs/common';
import { Cat } from 'src/cat.interface';

@Injectable()
export class CatsService {
private readonly cats: Cat[] = [];

create(cat: Cat) {
this.cats.push(cat);
}

findAll() {
return this.cats;
}
}

顺便修改src/cat.interface.ts,代码如下

1
2
3
4
5
export interface Cat {
name: string;
age: number;
bread: string;
}

下面看下如何在Controller中应用

1
2
3
4
5
6
7
8
9
10
11
12
13
constructor(private catsService: CatsService) {}

@Get()
async findAll(@Req() request: Request): Promise<Cat[]> {
return this.catsService.findAll();
}

@Post()
async create(@Body() createCatDto: CreateCatDto) {
this.catsService.create(createCatDto);
console.log(createCatDto);
return 'This action will create a new cat';
}

上面代码是主要关注的地方

另外需要导入对应的包

1
2
import { Cat } from 'src/cat.interface';
import { CatsService } from './cats.service';

然后访问http://127.0.0.1:3000/cats会得到一个空数组

执行下面的命令

1
$ curl -d 'name=durban1&age=12&bread=1' http://127.0.0.1:3000/cats

然后在访问http://127.0.0.1:3000/cats类似如下的数据

1
[{"name":"durban1","age":"12","bread":"1"},{"name":"durban1","age":"12","bread":"1"}]

学习记录 - If

题目


实现一个utils If,它接受条件C,True返回类型T,以及False返回类型F。 C可以是True或False,而T和F可以是任何类型。

比如

1
2
type A = If<true, 'a', 'b'>  // expected to be 'a'
type B = If<false, 'a', 'b'> // expected to be 'b'

测试用例


1
2
3
4
5
6
7
8
9
import { Equal, Expect } from '@type-challenges/utils'

type cases = [
Expect<Equal<If<true, 'a', 'b'>, 'a'>>,
Expect<Equal<If<false, 'a', 2>, 2>>,
]

// @ts-expect-error
type error = If<null, 'a', 'b'>

答案


1
type If<C, T, F> = C extends true ? T : F

学习记录 - 实现内置的Exclude <T,U>

题目简介


实现内置的Exclude <T,U>

从T中排除可分配给U的那些类型


测试用例


1
2
3
4
5
6
7
import { Equal, Expect, ExpectFalse, NotEqual } from '@type-challenges/utils'

type cases = [
Expect<Equal<MyExclude<"a" | "b" | "c", "a">, Exclude<"a" | "b" | "c", "a">>>,
Expect<Equal<MyExclude<"a" | "b" | "c", "a" | "b">, Exclude<"a" | "b" | "c", "a" | "b">>>,
Expect<Equal<MyExclude<string | number | (() => void), Function>, Exclude<string | number | (() => void), Function>>>,
]

答案


1
type MyExclude<T, U> = T extends U ? never : T;

学习记录 - 获取元素长度 - 对于给定的元组,您需要创建一个通用的Length,选择元组的长度

题目简介


对于给定的元组,您需要创建一个通用的Length,选择元组的长度

例如

1
2
3
4
5
type tesla = ['tesla', 'model 3', 'model X', 'model Y']
type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT']

type teslaLength = Length<tesla> // expected 4
type spaceXLength = Length<spaceX> // expected 5

测试用例


1
2
3
4
5
6
7
8
9
import { Equal, Expect } from '@type-challenges/utils'

const tesla = ['tesla', 'model 3', 'model X', 'model Y'] as const
const spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT'] as const

type cases = [
Expect<Equal<Length<typeof tesla>, 4>>,
Expect<Equal<Length<typeof spaceX>, 5>>,
]

答案


1
type Length<T extends any> = T extends ArrayLike<any> ? T["length"] : never

第一个元素 - 实现一个通用First<T>,它接受一个数组T并返回它的第一个元素的类型。学习记录如下


题目简介


实现一个通用First<T>,它接受一个数组T并返回它的第一个元素的类型。

例如

1
2
3
4
5
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]

type head1 = First<arr1> // expected to be 'a'
type head2 = First<arr2> // expected to be 3

测试用例


1
2
3
4
5
6
7
8
import { Equal, Expect } from '@type-challenges/utils'

type cases = [
Expect<Equal<First<[3, 2, 1]>, 3>>,
Expect<Equal<First<[() => 123, { a: string }]>, () => 123>>,
Expect<Equal<First<[]>, never>>,
Expect<Equal<First<[undefined]>, undefined>>
]

答案


1
type First<T extends any[]> = T extends never[] ? never : T[0]
0%