如何为每个MongoDB文档创建URL

时间:2017-07-17 01:53:31

标签: javascript node.js mongodb mongoose

我正在尝试使用快速路由将URL映射到我在数据库中的每个文档中的特定值。

例如。在Schema company.name中将返回公司名称。

有没有办法将company.name的值设置为唯一的URL?

我想到的唯一方法是创建循环或使用mongoose的.find迭代数据库并将其设置为

Thank you for using GitLab Docker Image!
Current version: gitlab-ce=9.3.6-ce.0

Configure GitLab for your system by editing /etc/gitlab/gitlab.rb file
And restart this container to reload settings.
To do it use docker exec:

  docker exec -it gitlab vim /etc/gitlab/gitlab.rb
  docker restart gitlab

For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

If this container fails to start due to permission problems try to fix it by executing:

  docker exec -it gitlab update-permissions
  docker restart gitlab

Installing gitlab.rb config...
Generating ssh_host_rsa_key...
Generating public/private rsa key pair.
Your identification has been saved in /etc/gitlab/ssh_host_rsa_key.
Your public key has been saved in /etc/gitlab/ssh_host_rsa_key.pub.
The key fingerprint is:
SHA256:GyFlf9tl7ZuEbuE+dwZUYiyahdsRzpC1T7kwyUvoD+o root@gitlab.example.com
The key's randomart image is:
+---[RSA 2048]----+
|        o .+oo   |
|       o .o*+o+.o|
|      . . o*@+oo+|
|       . o+o.Oo= |
|        S o o++..|
|         + oo + o|
|        o   .+ + |
|       .    o. .o|
|        E    .o..|
+----[SHA256]-----+
Generating ssh_host_ecdsa_key...
Generating public/private ecdsa key pair.
Your identification has been saved in /etc/gitlab/ssh_host_ecdsa_key.
Your public key has been saved in /etc/gitlab/ssh_host_ecdsa_key.pub.
The key fingerprint is:
SHA256:Kb99jG8EtMuTSdIuqBT3GLeD1D0wwTEcQhKgVJUlBjs root@gitlab.example.com
The key's randomart image is:
+---[ECDSA 256]---+
| .o+=*=+=+       |
|..  oo..=..      |
|.  E   . * .     |
|    o + +.B      |
|     +.BS* *     |
|    . +o= B .    |
|   . .  .o =     |
|    .    o. +    |
|        . .+.    |
+----[SHA256]-----+
Generating ssh_host_ed25519_key...
Generating public/private ed25519 key pair.
Your identification has been saved in /etc/gitlab/ssh_host_ed25519_key.
Your public key has been saved in /etc/gitlab/ssh_host_ed25519_key.pub.
The key fingerprint is:
SHA256:lVxpu0UoyNPWVY6D9c+m/bUTyvKP6vuR4cTOYwQ0j+U root@gitlab.example.com
The key's randomart image is:
+--[ED25519 256]--+
|       . o +.=o..|
|        +.=o@o.+ |
|         o+=.Eo o|
|         .  + .o.|
|        S    B  +|
|            B o= |
|            .Oo +|
|           ..o+.+|
|          .+*+.oo|
+----[SHA256]-----+
Preparing services...
Starting services...
Configuring GitLab package...
/opt/gitlab/embedded/bin/runsvdir-start: line 24: ulimit: pending signals: cannot modify limit: Operation not permitted
/opt/gitlab/embedded/bin/runsvdir-start: line 34: ulimit: max user processes: cannot modify limit: Operation not permitted
/opt/gitlab/embedded/bin/runsvdir-start: line 37: /proc/sys/fs/file-max: Read-only file system
Configuring GitLab...

================================================================================
Error executing action `run` on resource 'ruby_block[directory resource: /var/opt/gitlab/git-data]'
================================================================================

Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Failed asserting that ownership of "/var/opt/gitlab/git-data" was git
---- Begin output of set -x && [ "$(stat --printf='%U' $(readlink -f /var/opt/gitlab/git-data))" = 'git' ] ----
STDOUT:
STDERR: + readlink -f /var/opt/gitlab/git-data
+ stat --printf=%U /var/opt/gitlab/git-data
+ [ UNKNOWN = git ]
---- End output of set -x && [ "$(stat --printf='%U' $(readlink -f /var/opt/gitlab/git-data))" = 'git' ] ----
Ran set -x && [ "$(stat --printf='%U' $(readlink -f /var/opt/gitlab/git-data))" = 'git' ] returned 1

Cookbook Trace:
---------------
/opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/libraries/storage_directory_helper.rb:124:in `validate_command'
/opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/libraries/storage_directory_helper.rb:112:in `block in validate'
/opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/libraries/storage_directory_helper.rb:111:in `each_index'
/opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/libraries/storage_directory_helper.rb:111:in `validate'
/opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/libraries/storage_directory_helper.rb:87:in `validate!'
/opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/definitions/storage_directory.rb:35:in `block (3 levels) in from_file'

Resource Declaration:
---------------------
# In /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/definitions/storage_directory.rb

 26:   ruby_block "directory resource: #{params[:path]}" do
 27:     block do
 28:       # Ensure the directory exists
 29:       storage_helper.ensure_directory_exists(params[:path])
 30:
 31:       # Ensure the permissions are set
 32:       storage_helper.ensure_permissions_set(params[:path])
 33:
 34:       # Error out if we have not achieved the target permissions
 35:       storage_helper.validate!(params[:path])
 36:     end
 37:     not_if { storage_helper.validate(params[:path]) }
 38:   end
 39: end

Compiled Resource:
------------------
# Declared in /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/definitions/storage_directory.rb:26:in `block in from_file'

ruby_block("directory resource: /var/opt/gitlab/git-data") do
  params {:path=>"/var/opt/gitlab/git-data", :owner=>"git", :group=>nil, :mode=>"0700", :name=>"/var/opt/gitlab/git-data"}
  action [:run]
  retries 0
  retry_delay 2
  default_guard_interpreter :default
  block_name "directory resource: /var/opt/gitlab/git-data"
  declared_type :ruby_block
  cookbook_name "gitlab"
  recipe_name "gitlab-shell"
  block #<Proc:0x000000054a99a8@/opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/definitions/storage_directory.rb:27>
  not_if { #code block }
end

Platform:
---------
x86_64-linux

但是我不确定如何从路由文件访问数据库集合。

使用

app.get('/' + name , function (req , res ) {
   res.render('company.jade');
});

不返回数据库,因此不确定该怎么做。

我对Mongoose或node.js并不熟悉。真的只需要做这一件事,应用程序的其余部分是以我更熟悉的角度完成的。

希望我在这里有所了解。我确实有应用程序的代码,但它是来自app.js和routes.js的所有基本代码。如果它有用,我可以添加它。

1 个答案:

答案 0 :(得分:1)

您正在寻找Mongoose findOne query

const Company = require('.models/company');

app.get('/:name', function(req, res){
Company.findOne({'company.name': req.params.name}) //company.name depends upon the schema you set.
 .then(function(company){
   if (company)
    return res.send({success: true, data: company}); //return company as an object containing the queried company document.

   else 
    return res.send({success: false, error: 'Company not found'});

 })
 .catch(function(err){
  if (err) res.send({success: false, error: err}); 
 });
});