【问题标题】:unable to get collection names with mongoose无法使用 mongoose 获取集合名称
【发布时间】:2015-05-21 04:09:40
【问题描述】:

这段 Ruby 代码打印出 "db" 中的集合名称。尝试对咖啡做同样的事情

require 'mongo'
include Mongo
mongocl = MongoClient.new("localhost")
p mongocl["db"].collection_names
mongocl.close

等效的咖啡脚本代码(不确定)。

mongoose = require 'mongoose'
mongoose.connect 'localhost/db'
mongoose.connection.on 'open', (err) ->
  mongoose.connection.db.collectionNames (err, data) ->
    console.log data
    return 
mongoose.connection.close()

有人能指出其中的错误吗? 我正在使用最新的 mongoose@3.8.x

【问题讨论】:

    标签: node.js mongodb coffeescript mongoose


    【解决方案1】:

    问题是您在open 事件处理程序有机会执行之前关闭了连接。

    在传递给collectionNames的回调中移动close()调用:

    mongoose = require 'mongoose'
    mongoose.connect 'localhost/db'
    mongoose.connection.on 'open', (err) ->
      mongoose.connection.db.collectionNames (err, data) ->
        console.log data
        mongoose.connection.close()
    

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 2011-11-21
      • 1970-01-01
      • 2017-09-06
      • 2014-09-22
      • 2021-07-15
      • 2013-07-22
      • 2012-06-14
      • 2013-08-29
      相关资源
      最近更新 更多