在不知道架构/集合名称的情况下获取 Mongoose 架构和文档?
简短版本:我基本上想做 show collections
在 mongo shell、mongoose 中所做的事情。
长版本:
我想创建一个应用程序,允许用户基本上创建自己的架构>收藏>使用 mongo + mongoose + node.js 的文档。
该应用程序的第一步是显示以前创建的集合及其文档。但由于这些是由用户创建的,所以他们的名字是未知的。 我发现最接近的代码是:
function find (collec, query, callback) {
mongoose.connection.db.collection(collec, function (err, collection) {
collection.find(query).toArray(callback);
});
}
但我不知道集合的名称是什么,所以我无法传递“collec”参数。
那么,有人知道如何在不知道藏品名称的情况下获取藏品列表吗?
Short Version: I basically want to do what show collections
does in the mongo shell, in mongoose.
Long Version:
I want to create an application that allows users to basically create their own Schema's > Collections > Documents using mongo + mongoose + node.js.
The first step of this application would be to show previously created Collections and their Documents. But since these would be created by a user their names are not known.
The code I've found that came closest was:
function find (collec, query, callback) {
mongoose.connection.db.collection(collec, function (err, collection) {
collection.find(query).toArray(callback);
});
}
But I don't know what the name of the collection is so I can't pass the 'collec' parameter.
So, anyone know how to get the list of collections without knowing their names?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终使用 node-mongodb-native ( mongoose 在其上工作的 mongo 驱动程序)来获取集合名称:
mongoose.connect()
返回的对象没有类似于的方法我可以找到的 collectionNames()
。I ended up using node-mongodb-native (the mongo driver that mongoose works on top of) to get the collection names:
The Object returned by
mongoose.connect()
has no method similar tocollectionNames()
that I can find.我对 mongoose 一无所知,但此页面建议您可以使用 mongoose.connection.collections 访问所有集合:http://mongoosejs.com/docs/api.html
每个集合再次应该有一个 name 属性。
I know nothing of mongoose, but this page suggests that you could use
mongoose.connection.collections
to access all collections: http://mongoosejs.com/docs/api.htmleach collection again should have a name attribute.
试试这个:
try this: