【问题标题】:Expressjs Mongoose find nested embedded documents are undefinedExpressjs Mongoose 发现嵌套的嵌入文档未定义
【发布时间】:2012-06-17 21:26:33
【问题描述】:

我正在使用 nodejs 0.6.18 expressjs 2.5.8 和 mongoose 2.6.7 构建游戏。

我正在尝试使用 mongoose 存储和检索嵌入式文档。 考虑这个例子:

用户架构

var User = module.exports = new Schema({
      username: { type: String }
    , characters: [Character]
    , created: { type: Date, default: Date.now }
});

字符架构

var Character = module.exports = new Schema({
      name: { type: String }
    , spells: [Spell]
    , created: { type: Date, default: Date.now }
});

拼写模式

var Spell = module.exports = new Schema({
      name: { type: String }
    , created { type: Date, default: Date.now }
});

猫鼬

var db = mongoose.createConnection('mongodb://localhost/mygame');
mongoose.model('User', require('./models/user'));
mongoose.model('Character', require('./models/character'));
mongoose.model('Spell', require('./models/spell')

路线

app.get('/', function(req, res) {
  var User = db.model('User')
    , Character = db.model('Character')
    , Spell = db.model('Spell')
    , u = new User({username:'foo'})
    , c = new Character({name:'bar'});

  c.spells.push(new Spell({name:'fireball'}));
  c.spells.push(new Spell({name:'frozenball'}));
  u.characters.push(c);

  u.save(function(e, s) {
    User.find({username:'foo'}, function(err, success) {
      console.error(err);
      console.log(success);
    });
  });
});

控制台输出

null
[ { username: 'foo',
    _id: 4fda2c77faa9aa5c68000003,
    created: Thu, 14 Jun 2012 18:24:55 GMT,
    characters: [ undefined ] } ]

看起来用户文档已被猫鼬正确保存和检索。但是,相关的嵌入文档是未定义

我想确保我的文档被保存,所以我直接向 mongodb 询问:

$ mongo mygame
> db.users.find({username:'foo'})
{
    "username" : "foo",
    "_id" : ObjectId("4fda2c77faa9aa5c68000003"),
    "created" : ISODate("2012-06-14T18:24:55.982Z"),
    "characters" : [
        {
            "name" : "bar",
            "_id" : ObjectId("4fda2c77faa9aa5c68000004"),
            "created" : ISODate("2012-06-14T18:24:55.986Z"),
            "spells" : [
                {
                    "name" : "fireball",
                    "_id" : ObjectId("4fda2c77faa9aa5c68000005"),
                    "created" : ISODate("2012-06-14T18:24:55.987Z")
                },
                {
                    "name" : "frozenball",
                    "_id" : ObjectId("4fda2c77faa9aa5c68000007"),
                    "created" : ISODate("2012-06-14T18:24:55.990Z")
                }
            ]
        }
    ]
}

如您所见,我的文档似乎已正确存储到 mongodb,但我无法检索谁嵌入了 mongoose。

我也尝试过不使用嵌套的嵌入式 Spell 文档,这会产生完全相同的问题。

编辑

@pat 你是对的。如果我将所有 Schema 以正确的顺序直接放在同一个文件(例如主 app.js)中,它就可以工作。

事实是,我希望尽可能将每个模型保存在单独的文件中(它们会增长很多)。

例如,我的用户模型包含在名为 models/user.js 的文件中,应该可以使用module.exports 访问,如上。

但是,当我尝试将我的模型链接到另一个文件中的 mongoose 时:mongoose.model('User', require('./models/user')); mongoose find 方法返回未定义的嵌入文档。

您对如何正确地将我的猫鼬模型保存在单独的文件中有任何想法吗?

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    我相信,用户模式需要在字符和拼写之后声明。

    【讨论】:

    • 感谢您的回答,对您有所帮助。但我想将模型保存在单独的文件中,我已经编辑了我的问题。
    【解决方案2】:

    用户架构文件应该首先需要顶部的 CharacterSchema,然后将其传入:

    var CharacterSchema = require('./character');
    
    var User = module.exports = new Schema({
          username: { type: String }
        , characters: [CharacterSchema]
        , created: { type: Date, default: Date.now }
    });
    

    同样,CharacterSchema 文件应该首先需要顶部的 SpellSchema,然后传递它:

    var SpellSchema = require('./spell');
    var CharacterSchema = module.exports = new Schema({ spells: [SpellSchema] })
    

    这将保留他们的顺序。

    注意:子文档并不是真正需要成为模型,因为模型被映射到集合,但只要您不直接在子文档上调用 save,它们就不会被保存到数据库中的单独集合中。

    【讨论】:

    • 谢谢。我的问题来自依赖问题以及错误的文件结构。最后,我通过 ObjectIds 加入我的模型,而不是嵌入文档,这似乎更适合我的需求。
    猜你喜欢
    • 2012-04-16
    • 2012-06-23
    • 2016-04-25
    • 2014-01-29
    • 2020-05-02
    • 2018-11-09
    • 2015-01-06
    • 1970-01-01
    • 2015-07-17
    相关资源
    最近更新 更多