mongoose 查询不出来数据怎么办? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
w88975

mongoose 查询不出来数据怎么办?

  •  2
     
  •   w88975 2015 年 8 月 17 日 4034 次点击
    这是一个创建于 3904 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是代码

    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 (); }); 

    查询出来的数据为 [],不知道原因出在哪里,求大大解决

    17 条回复    2015-08-18 01:39:38 +08:00
    minvacai
        1
    minvacai  
       2015 年 8 月 17 日
    不会 js ,不过你的 find 里那个 if/else 有必要么...
    w88975
        2
    w88975  
    OP
       2015 年 8 月 17 日
    @minvacai .... 写错了而已,不要在意这些细节 err 为空
    minvacai
        3
    minvacai  
       2015 年 8 月 17 日
    话说,你 result 是哪里来的, find 里没有具体的查询语句,也没见哪里调用 find 了啊
    w88975
        4
    w88975  
    OP
       2015 年 8 月 17 日
    @minvacai 这是 mongoose 的查询,表在
    ``var mOngooseModel= db.model ('download', downloadSchema );``

    db.model ('download'.... 这里的 download 是表
    Dongdong36
        5
    Dongdong36  
       2015 年 8 月 17 日
    数据库中的集合名称叫什么
    minvacai
        6
    minvacai  
       2015 年 8 月 17 日

    我是用 pymongo 的,还是不胡说八道给你添乱了
    Dongdong36
        7
    Dongdong36  
       2015 年 8 月 17 日
    @w88975 集合名确认一下是否为“ downloads ”
    chaim
        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 为空
    }
    });
    });

    我理解大致是这样子的
    w88975
        9
    w88975  
    OP
       2015 年 8 月 17 日
    @Dongdong36 download 这个表存在
    gyteng
        10
    gyteng  
       2015 年 8 月 17 日
    if (error ) {
    console.log (result );
    } else {
    console.log (result );
    }
    db.close ();
    楼主错在这个地方
    whimsySun
        11
    whimsySun  
       2015 年 8 月 17 日   1
    mongoose 的 collection ,不指定名字的话,默认为 model name + s ,你这里应该是 downloads
    w88975
        12
    w88975  
    OP
       2015 年 8 月 17 日
    @whimsySun collection 是提前创建好的,不是用 mogoose 创建的
    w88975
        13
    w88975  
    OP
       2015 年 8 月 17 日
    @gyteng 这里把 err 写成 result 了 错并不在这里
    whimsySun
        14
    whimsySun  
       2015 年 8 月 17 日
    `new Scheme ({...define}, {collection: 'download'})` 这样指定`collection`名
    jinwyp
        15
    jinwyp  
       2015 年 8 月 18 日
    mongooseModel.find (function (error,result ){


    应该为
    mongooseModel.find ({}, function (error,result ){

    第一个参数是查询条件, 你直接漏了
    faceair
        16
    faceair  
       2015 年 8 月 18 日
    楼上说的是对的 conditions 参数不能省略
    w88975
        17
    w88975  
    OP
       2015 年 8 月 18 日
    @faceair 谢谢仁兄的方法,在 Scheme 你指定了 collection 名后,查询正常了,正解。√
    @jinwyp @faceair 经过测试,发现不用{},直接省略这个参数也可以全部查询,不过为了代码可读性,还是按你的写法来了。

    谢谢大家
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2904 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 57ms UTC 03:17 PVG 11:17 LAX 20:17 JFK 23:17
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86