
这是代码
var mongoose = require ('mongoose'); var db = mongoose.createConnection ('mongodb://xxxxxxxxxxxxxxxxx/test'); db.on ('error', function (error ) { console.log (error ); }); var downloadSchema = new mongoose.Schema ({ assets : {type: String}, body : {type: String}, tag_name : {type: String}, date : {type: String}, }); var mongooseModel = db.model ('download', downloadSchema ); mongooseModel.find (function (error,result ){ if (error ) { console.log (result ); } else { console.log (result ); // result 为空 } db.close (); }); 查询出来的数据为 [],不知道原因出在哪里,求大大解决
1 minvacai 2015 年 8 月 17 日 不会 js ,不过你的 find 里那个 if/else 有必要么... |
3 minvacai 2015 年 8 月 17 日 话说,你 result 是哪里来的, find 里没有具体的查询语句,也没见哪里调用 find 了啊 |
5 Dongdong36 2015 年 8 月 17 日 数据库中的集合名称叫什么 |
6 minvacai 2015 年 8 月 17 日 汗 我是用 pymongo 的,还是不胡说八道给你添乱了 |
7 Dongdong36 2015 年 8 月 17 日 @w88975 集合名确认一下是否为“ downloads ” |
8 chaim 2015 年 8 月 17 日 mongoose.connect ('mongodb://xxxxxxxxxxxxxxxxx/test'); var downloadSchema = new mongoose.Schema ({ assets : {type: String}, body : {type: String}, tag_name : {type: String}, date : {type: String}, }); var mOngooseModel= mongoose.model ('download', downloadSchema ); mongoose.connection.on ('connected', function () { mongooseModel.find (function (error,result ){ if (error ) { console.log (result ); } else { console.log (result ); // result 为空 } }); }); 我理解大致是这样子的 |
9 w88975 OP @Dongdong36 download 这个表存在 |
10 gyteng 2015 年 8 月 17 日 if (error ) { console.log (result ); } else { console.log (result ); } db.close (); 楼主错在这个地方 |
11 whimsySun 2015 年 8 月 17 日 mongoose 的 collection ,不指定名字的话,默认为 model name + s ,你这里应该是 downloads |
14 whimsySun 2015 年 8 月 17 日 `new Scheme ({...define}, {collection: 'download'})` 这样指定`collection`名 |
15 jinwyp 2015 年 8 月 18 日 mongooseModel.find (function (error,result ){ 应该为 mongooseModel.find ({}, function (error,result ){ 第一个参数是查询条件, 你直接漏了 |
16 faceair 2015 年 8 月 18 日 楼上说的是对的 conditions 参数不能省略 |