Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8109187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:20:23+00:00 2026-06-06T01:20:23+00:00

I’m building a game with nodejs 0.6.18 expressjs 2.5.8 and mongoose 2.6.7. I’m trying

  • 0

I’m building a game with nodejs 0.6.18 expressjs 2.5.8 and mongoose 2.6.7.

I’m trying to store and retrieve embedded documents with mongoose.
Consider this example :

User schema

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

Character schema

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

Spell schema

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

Mongoose

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')

Route

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

Console output

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

It looks like the User document is correctly saved and retrieved by mongoose. But, associated embedded documents are undefined.

I wanted to be sure that my document is saved, so I’ve directly asked to 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")
                }
            ]
        }
    ]
}

As you can see, my documents seems to be correctly stored to mongodb, but I’m unable to retrieve whose who are embedded with mongoose.

I’ve also tried without the nested embedded Spell document, wich produce the exact same problem.

EDIT

@pat You are right. If I put all the Schemas directly in the same file (the main app.js for exemple) with the right order, it works.

The fact is, I would like to keep each models in separate files as much as possible (they are gonna grow a lot).

For exemple, my User model is contained in a file called models/user.js, and should be accessible using module.exports as above.

But, when I try to link my model to mongoose in an another file : mongoose.model('User', require('./models/user')); the mongoose find method returns undefined embedded documents.

Do you have any ideas on how to properly keep my mongoose models on separate files ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:20:24+00:00Added an answer on June 6, 2026 at 1:20 am

    The user schema file should first require the CharacterSchema at the top, then pass it in:

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

    Likewise, the CharacterSchema file should first require the SpellSchema at the top, then pass it:

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

    This will retain their order.

    Note: subdocs are not really needed to be models, since models are mapped to collections, but as long as you are not calling save directly on your subdocs they won’t get saved to a separate collection in the db.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.