V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
w88975
V2EX  ›  Node.js

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

  •  2
     
  •   w88975 · 2015-08-17 18:54:35 +08:00 · 3483 次点击
    这是一个创建于 3179 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是代码

    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-08-17 19:15:38 +08:00
    不会 js ,不过你的 find 里那个 if/else 有必要么...
    w88975
        2
    w88975  
    OP
       2015-08-17 19:35:25 +08:00
    @minvacai .... 写错了而已,不要在意这些细节 err 为空
    minvacai
        3
    minvacai  
       2015-08-17 19:56:49 +08:00
    话说,你 result 是哪里来的, find 里没有具体的查询语句,也没见哪里调用 find 了啊
    w88975
        4
    w88975  
    OP
       2015-08-17 20:00:01 +08:00
    @minvacai 这是 mongoose 的查询,表在
    ``var mongooseModel = db.model ('download', downloadSchema );``

    db.model ('download'.... 这里的 download 是表
    Dongdong36
        5
    Dongdong36  
       2015-08-17 20:32:16 +08:00
    数据库中的集合名称叫什么
    minvacai
        6
    minvacai  
       2015-08-17 20:32:54 +08:00

    我是用 pymongo 的,还是不胡说八道给你添乱了
    Dongdong36
        7
    Dongdong36  
       2015-08-17 20:33:09 +08:00
    @w88975 集合名确认一下是否为“ downloads ”
    chaim
        8
    chaim  
       2015-08-17 20:51:10 +08:00
    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-08-17 22:55:03 +08:00
    @Dongdong36 download 这个表存在
    gyteng
        10
    gyteng  
       2015-08-17 23:22:10 +08:00
    if (error ) {
    console.log (result );
    } else {
    console.log (result );
    }
    db.close ();
    楼主错在这个地方
    whimsySun
        11
    whimsySun  
       2015-08-17 23:44:20 +08:00   ❤️ 1
    mongoose 的 collection ,不指定名字的话,默认为 model name + s ,你这里应该是 downloads
    w88975
        12
    w88975  
    OP
       2015-08-17 23:45:34 +08:00
    @whimsySun collection 是提前创建好的,不是用 mogoose 创建的
    w88975
        13
    w88975  
    OP
       2015-08-17 23:46:00 +08:00
    @gyteng 这里把 err 写成 result 了 错并不在这里
    whimsySun
        14
    whimsySun  
       2015-08-17 23:46:39 +08:00
    `new Scheme ({...define}, {collection: 'download'})` 这样指定`collection`名
    jinwyp
        15
    jinwyp  
       2015-08-18 00:12:25 +08:00
    mongooseModel.find (function (error,result ){


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

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

    谢谢大家
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   999 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 18:32 · PVG 02:32 · LAX 11:32 · JFK 14:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.