如何使用mongoose从mongodb返回所有文件?

时间:2016-08-06 18:49:32

标签: mongodb mongoose

常规的猫鼬发现似乎不像json那样发回文件。 Eeach doc是我的数据库是一个简单的对象。

<#
# .SYNOPSIS
#  Clears the command history, including the saved-to-file history, if applicable.
#>
function Clear-SavedHistory {
  [CmdletBinding(ConfirmImpact='High', SupportsShouldProcess)]
  param(    
  )

  # Debugging: For testing you can simulate not having PSReadline loaded with
  #            Remove-Module PSReadline -Force
  $havePSReadline = ($null -ne (Get-Module -EA SilentlyContinue PSReadline))

  Write-Verbose "PSReadline present: $havePSReadline"

  $target = if ($havePSReadline) { "entire command history, including from previous sessions" } else { "command history" } 

  if (-not $pscmdlet.ShouldProcess($target))
  {
        return
  }

  if ($havePSReadline) {

    Clear-Host

    # Remove PSReadline's saved-history file.
    if (Test-Path (Get-PSReadlineOption).HistorySavePath) { 
      # Abort, if the file for some reason cannot be removed.
      Remove-Item -EA Stop (Get-PSReadlineOption).HistorySavePath 
      # To be safe, we recreate the file (empty). 
      $null = New-Item -Type File -Path (Get-PSReadlineOption).HistorySavePath
    }

    # Clear PowerShell's own history 
    Clear-History

    # Clear PSReadline's *session* history.
    # General caveat (doesn't apply here, because we're removing the saved-history file):
    #   * By default (-HistorySaveStyle SaveIncrementally), if you use
    #    [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory(), any sensitive
    #    commands *have already been saved to the history*, so they'll *reappear in the next session*. 
    #   * Placing `Set-PSReadlineOption -HistorySaveStyle SaveAtExit` in your profile 
    #     SHOULD help that, but as of PSReadline v1.2, this option is BROKEN (saves nothing). 
    [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()

  } else { # Without PSReadline, we only have a *session* history.

    Clear-Host

    # Clear the doskey library's buffer, used pre-PSReadline. 
    # !! Unfortunately, this requires sending key combination Alt+F7.
    # Thanks, https://stackoverflow.com/a/13257933/45375
    $null = [system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")
    [System.Windows.Forms.SendKeys]::Sendwait('%{F7 2}')

    # Clear PowerShell's own history 
    Clear-History

  }

}

我假设是因为猫鼬没有用json回应,而是我相信一系列文件。但是我如何映射这些以发送回json并最终在视角中以角度进行循环。

这是我的路线

{
    "_id" : ObjectId("57a170f0736435d829d97c17"),
    "ip" : "74.208.147.41",
    "domain" : "test.com",
    "wp" : "4.4.2",
    "host_name" : "Web03",
    "hosted" : 1.0
}

修改 它似乎肯定是mongoose查询。如果我传入“ip”字段的值来查询(例如:Site.find({ip:'74 .208.147.41'})...,我得到一个回复​​。当我没有传递任何值时,我不会得到回应。

这是我的模特:

router.get('/api/sites', function(req, res, next) {
  Site.find({}, function(err, sites) {
    if (err) {
      next(err)
    } else {
      return res.json({sites: sites});
    }
  });
});

编辑8/10/16: 最后更新了我的mongoose版本,查找查询工作正常。

编辑2/01/17: 明确定义要使用的集合是真正的问题。这个帖子帮助了我: Mongoose always returning an empty array NodeJS

0 个答案:

没有答案