小编mbu*_*ann的帖子

MongoDB由数组内部元素组成

我有一个文章列表,每个文章都有一个数组属性,列出了其中提到的各个人:

_id: {
    $oid: "52b632a9e4f2ba13c82ccd23"
},
providerName: "The Guardian",
url: "http://feeds.theguardian.com/c/34708/f/663860/s/3516cebc/sc/38/l/0L0Stheguardian0N0Cmusic0C20A130Cdec0C220Cwaterboys0Efishermans0Eblues0Etour0Ehammersmith/story01.htm",
subject: "The Waterboys – review",
class_artist: [
    "paul mccartney"
]
Run Code Online (Sandbox Code Playgroud)

我一直在努力(不成功)class_artist根据他们在过去7天内被标记的文章数量来获取所有个体艺术家的列表().

我已经达到了:

var date = new Date();
date.setDate(date.getDate() - 7);

db.articles.group({
    key: { class_artist: 1 },
    cond: { class_date: { $gt: date } },
    reduce: function ( curr, result ) { result.cnt++; },
    initial: { cnt : 0 }
}).sort({cnt: -1});
Run Code Online (Sandbox Code Playgroud)

但不幸的是,它并不是基于单个数组值来计算它们,而是基于数组合成(即艺术家列表).

我尝试使用该$unwind功能,但无法使其工作.

mongodb mongodb-query aggregation-framework

38
推荐指数
1
解决办法
4万
查看次数

如何避免单页应用程序中的 CORS 预检请求?

我想构建一个单页应用程序(SPA),并在后端 API(REST)和前端资产(静态 vue.js 代码)分离期间注意到以下问题:

当从 API 后端以外的其他域提供 index.html 时,大多数 POST/PUT 请求都会触发 CORS 预检请求。

我做了一些研究,发现博客文章 [1][2] 讨论了这个问题 - 没有给出实际的解决方案。某些标头(例如,授权标头和具有application/json值的 Content-Type 标头)不允许作为 cors-safelisted-request-header。因此,POST/PUT 请求会触发 CORS 预检请求。这是不可接受的,因为它增加了大量的延迟。

问题

如果两个域都属于同一实体,是否可以避免这些预检请求?

研究

我做了一些关于如何避免前端和后端之间的 CORS 请求的研究。该解决方案需要从与 REST API 后端相同的域提供 index.html 文件(请参阅下面的示例)。我想知道不使用单独的域是否是避免 SPA 的 CORS 请求的唯一解决方案。

场景(示例)

  • 单页应用程序(SPA);前端和后端层
  • 托管在AWS云中
  • 第 1 层:具有 S3 存储桶源的 CloudFront CDN - 在static.example.com上提供静态资产(Vue.js 前端)
  • 第 2 层:具有 ECS 集成的负载均衡器,运行 node.js 容器来托管example.com上的 (REST) 后端
  • 第 1 层和第 2 层之间的通信使用 HTTPS 协议和 REST 范例。
  • index.html 由第 2 层提供服务,客户使用example.com …

architecture amazon-web-services cors single-page-application

8
推荐指数
2
解决办法
2099
查看次数

有没有办法让protoc-gen-go中json的键名小驼峰命名?

当使用 protoc-gen-go 从 proto 文件生成 go 代码时,json 的键名称将保留为 proto 文件中指定的键。在官方字段名中推荐使用snake case。 https://developers.google.com/protocol-buffers/docs/style

但是,我希望 json 的键名称为蛇形命名法。

当我检查 protoc-gen-go 的生成器代码时,我当然按原样设置了字段名称。

jsonName := *field.Name
tag := fmt.Sprintf("protobuf:%s json:%q", g.goTag(message, field, wiretype), jsonName+",omitempty")
Run Code Online (Sandbox Code Playgroud)

这对我来说是理想的解决方案:

jsonName := field.GetJsonName()
Run Code Online (Sandbox Code Playgroud)

如何获取蛇形大小写的字段名称?

go protocol-buffers

5
推荐指数
1
解决办法
2493
查看次数

golang中强制关闭http连接

我的程序从一台服务器下载一个文件,然后将其返回给用户。这是它的片段:

// Make get request to target server
resp, httpErr := http.Get(url.String()) 

// Return error if http request is failed 
if httpErr != nil {
    fmt.Fprintln(w,"Http Request Failed :" ,httpErr.Error())
    return
}

//Setting up headers
w.Header().Set("Content-Disposition", "attachment; filename="+vid.Title+"."+format.Extension)
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
w.Header().Set("Content-Length", strconv.Itoa(int(resp.ContentLength)))

// Copy instream of resp.Body to writer
io.Copy(w, resp.Body)
Run Code Online (Sandbox Code Playgroud)

当用户停止下载或关闭连接时,我也想关闭 GET 连接。但正如我通过使用图发现的那样,它并没有关闭。如何关闭用户的连接?

http go

4
推荐指数
1
解决办法
1万
查看次数

在mac中安装mongo shell

我只想安装mongo-shell在我的 mac 上

我跑了brew update然后我跑了brew install mongodb-org-shell..但它没有被安装。下面是错误信息


bash-3.2$ brew install mongodb-org-shell 
Error: No available formula with the name "mongodb-org-shell" 
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get complete history run:
  git -C "$(brew --repo homebrew/core)" fetch --unshallow

Error: No previously deleted formula found.
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: …
Run Code Online (Sandbox Code Playgroud)

macos shell homebrew mongodb

4
推荐指数
2
解决办法
3753
查看次数

按键值过滤对象

我有一组对象可以说:

var ob=[
  {
    name:'john', 
    surname:'fox'
  }, {
    name:'jill',
    surname:'hog'
  }
];
Run Code Online (Sandbox Code Playgroud)

我正在网站上实现搜索,我可以在其中输入姓名或姓氏,并且它应该通过包含输入值的对象过滤新数组。
因此,如果我的输入是 'fox',它将过滤掉包含键值 'fox' 的对象

我的简单想法是:

ob.filter(item=>{ return item.name.includes(searchterm) || 
item.surname.includes(searchterm)}
Run Code Online (Sandbox Code Playgroud)

但我想有更好的方法,以防键名改变。

javascript key object filter

3
推荐指数
2
解决办法
2万
查看次数

Go语言安装

我只是按照 golang (ubuntu 16) 的安装指南进行操作。我在 /etc/usr 中提取了存档,在 /home/user/.profile 中添加了 env 变量,我刚刚在 hello world 代码上测试了基本的 go 构建。

我收到以下错误:

The program 'go' is currently not installed. You can install it by typing: sudo apt install golang-go
Run Code Online (Sandbox Code Playgroud)

为什么它要求我(再次?)安装它?

installation go go-build

2
推荐指数
2
解决办法
2594
查看次数

os.PathError 没有实现错误

PathError在 Golang 的os库中找到的类型:

type PathError struct {
    Op   string
    Path string
    Err  error
}

func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
Run Code Online (Sandbox Code Playgroud)

几乎实现了 Go 的error界面

type error interface {
    Error() string
}
Run Code Online (Sandbox Code Playgroud)

但是,当尝试将其作为错误传递时,您会收到以下编译时错误:

cannot use (type os.PathError) as type error in argument... 
os.PathError does not implement error (Error method has pointer receiver)
Run Code Online (Sandbox Code Playgroud)

为什么会os.PathError为 Error 方法使用指针接收器,而只是避免满足错误接口的要求?

完整示例:

package main

import ( …
Run Code Online (Sandbox Code Playgroud)

error-handling interface go

2
推荐指数
1
解决办法
1611
查看次数

Rails wikedPDF 错误:无法生成 PDF

我尝试在 Rails 6 上使用 WickedPdf 但出现此错误

RuntimeError (Failed to execute:
["/home/guilherme/.rbenv/versions/2.7.1/bin/wkhtmltopdf", "file:////tmp/wicked_pdf20200531-14069-p9pvre.html", "/tmp/wicked_pdf_generated_file20200531-14069-8mk29k.pdf"]
Error: PDF could not be generated!
 Command Error: /home/guilherme/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf:45:in `<top (required)>': Invalid platform, must be running on Ubuntu 14.04/16.04/18.04 CentOS 6/7/8, Debian 8/9/10, or intel-based macOS (missing binary: /home/guilherme/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf_ubuntu_20.04_amd64). (RuntimeError)
    from /home/guilherme/.rbenv/versions/2.7.1/bin/wkhtmltopdf:23:in `load'
    from /home/guilherme/.rbenv/versions/2.7.1/bin/wkhtmltopdf:23:in `<main>'
):
Run Code Online (Sandbox Code Playgroud)

在我的 gemfile 中我添加

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
Run Code Online (Sandbox Code Playgroud)

我渲染 pdf 的控制器方法是

  def gera_contrato
    cliente = Cliente.find_by_id(params[:cliente])
    @cnpj = cliente.local.cnpj
    @razao = cliente.local.razao
    @fantasia = cliente.local.fantasia
    @nome = cliente.nome
    @cpf = cliente.cpf …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

2
推荐指数
1
解决办法
4690
查看次数

gitlab-runner 命令用于重新启动运行程序的生命周期

我正在尝试配置一个跑步者并使用参考https://docs.gitlab.com/runner/commands/

但不清楚运行/启动/安装/注册的命令生命周期是什么

  • 应该在我重新启动系统时调用一次或每次调用 register。安装和启动怎么样?

  • 要重新启动跑步者,我应该怎么做stopuninstall然后install start run呢?

  • 我应该这样run做还是start会产生相同的效果?

gitlab gitlab-ci gitlab-ci-runner

1
推荐指数
1
解决办法
3952
查看次数

使用.net core和angular来托管Web应用程序的最佳方法是什么?

托管Web应用程序时,是否可以单独托管Web API和前端,例如:.Net Core Web API和Angular 6项目?

.net hosting .net-core angular

0
推荐指数
1
解决办法
65
查看次数