the*_*ham 122 mongoose mongodb node.js express
我question
在数据库中有300个对象的大集合test
.我可以通过MongoDB的交互式shell轻松地与这个集合进行交互; 但是,当我尝试通过Mongoose在express.js应用程序中获取集合时,我得到一个空数组.
我的问题是,如何访问这个已经存在的数据集而不是在Express中重新创建它?这是一些代码:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
mongoose.model('question', new Schema({ url: String, text: String, id: Number }));
var questions = mongoose.model('question');
questions.find({}, function(err, data) { console.log(err, data, data.length); });
Run Code Online (Sandbox Code Playgroud)
这输出:
null [] 0
Run Code Online (Sandbox Code Playgroud)
小智 232
Mongoose添加了在模式下指定集合名称的功能,或者在声明模型时作为第三个参数.否则,它将使用您映射到模型的名称给出的复数版本.
尝试类似以下内容的模式映射:
new Schema({ url: String, text: String, id: Number},
{ collection : 'question' }); // collection name
Run Code Online (Sandbox Code Playgroud)
或模型映射:
mongoose.model('Question',
new Schema({ url: String, text: String, id: Number}),
'question'); // collection name
Run Code Online (Sandbox Code Playgroud)
Mic*_*fen 58
如果有人只想要一个简单的复制粘贴插件功能,那么这就是Will Nathan的答案的抽象:
function find (name, query, cb) {
mongoose.connection.db.collection(name, function (err, collection) {
collection.find(query).toArray(cb);
});
}
Run Code Online (Sandbox Code Playgroud)
只find(collection_name, query, callback);
需要给出结果.
例如,如果我在集合'foo'中有一个文档{a:1}并且我想列出它的属性,我这样做:
find('foo', {a : 1}, function (err, docs) {
console.dir(docs);
});
//output: [ { _id: 4e22118fb83406f66a159da5, a: 1 } ]
Run Code Online (Sandbox Code Playgroud)
Leo*_*iro 20
你可以做这样的事情,而不是你将访问mongoose中的本机mongodb函数:
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/local');
var connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', function () {
connection.db.collection("YourCollectionName", function(err, collection){
collection.find({}).toArray(function(err, data){
console.log(data); // it will print your collection data
})
});
});
Run Code Online (Sandbox Code Playgroud)
Wil*_*han 15
我有同样的问题,并能够使用现有的Mongoose连接和下面的代码运行无模式查询.我添加了一个简单的约束'a = b'来显示你要添加这样一个约束的位置:
var action = function (err, collection) {
// Locate all the entries using find
collection.find({'a':'b'}).toArray(function(err, results) {
/* whatever you want to do with the results in node such as the following
res.render('home', {
'title': 'MyTitle',
'data': results
});
*/
});
};
mongoose.connection.db.collection('question', action);
Run Code Online (Sandbox Code Playgroud)
你确定你已连接到数据库吗?(我问,因为我没有看到指定的端口)
尝试:
mongoose.connection.on("open", function(){
console.log("mongodb is connected!!");
});
Run Code Online (Sandbox Code Playgroud)
此外,您可以在mongo shell中执行"show collections"以查看数据库中的集合 - 也许尝试通过mongoose添加记录并查看它最终的位置?
从连接字符串的外观来看,您应该在"test"db中看到记录.
希望能帮助到你!
小智 6
至少对我来说,其他不明显的事情是,当使用 Mongoose 的第三个参数来避免用同名的新集合替换实际集合时,new Schema(...)
实际上只是一个占位符,并且不会干扰现有集合架构所以
var User = mongoose.model('User', new Schema({ url: String, text: String, id: Number}, { collection : 'users' })); // collection name;
User.find({}, function(err, data) { console.log(err, data, data.length);});
Run Code Online (Sandbox Code Playgroud)
工作正常并返回所有字段 - 即使实际(远程)模式不包含这些字段。Mongoose 仍然希望它为new Schema(...)
,并且变量几乎肯定不会破解它。
归档时间: |
|
查看次数: |
104412 次 |
最近记录: |