1

I'm having a hard time trying to populate an array of sub-documents with MongooseJS. Could someone help?

Here is user.js:

var mongoose = require("mongoose"); var userSchema = new mongoose.Schema({ name: { type: String }, positions: { type: [mongoose.Schema.ObjectId], ref: 'position' } }); userSchema.statics.getUserByID = function (id, callback){ return this.findOne({_id: id}).populate('positions').exec(callback); }; var user = mongoose.model('user', userSchema); module.exports = user; 

And here is position.js

var mongoose = require("mongoose"); var positionSchema = new mongoose.Schema({ modifiedDate: { type: Date }, location: { type: [Number], index: '2d' } }); var position = mongoose.model('position', positionSchema); module.exports = position; 

The positions array of the user schema isn't being populated when getUserByID is called. Am I missing something obvious here?

EDIT

As per the answer below, I've changed my user schema to be:

var mongoose = require("mongoose"); var userSchema = new mongoose.Schema({ name: { type: String }, positions: [{ type: mongoose.Schema.ObjectId, ref: 'position' }] }); userSchema.statics.getUserByID = function (id, callback){ return this.findOne({_id: id}).populate('positions').exec(callback); }; var user = mongoose.model('user', userSchema); module.exports = user; 

I've also removed the location property from the position schema, in case there was something odd happening there.

Nothing I've tried seems to make any difference at all.

Here is what is currently saved in the users collection:

{ "__v": 1, "_id": { "$oid": "531f8ab89938130200433ef8" }, "modifiedDate": { "$date": "2014-03-11T22:14:43.174Z" }, "name": "Josh", "positions": [ { "$oid": "531f8ad39938130200433efa" } ] } 

And here is what's in the positions collection:

{ "modifiedDate": { "$date": "2014-03-11T22:14:43.163Z" }, "_id": { "$oid": "531f8ad39938130200433efa" }, "__v": 0 } 

For reference I'm using "mongoose": "^3.8.8" in my package.json file.

I'm really stumped by this, any help would be appreciated.

3 Answers 3

4

You made a small but important typo in your user schema - the type of position field should not be set to array of ObjectId. What you probably wanted is this schema :

var userSchema = new mongoose.Schema({ name: { type: String }, positions: [{ type: mongoose.Schema.ObjectId, ref: 'position' }] }); 
Sign up to request clarification or add additional context in comments.

5 Comments

Ah right that makes sense. I've seen examples where the property type is an array (like I had originally) and where the property is an array (like you've done it). When should each be used?
I think that setting the property type to an array of objects is invalid in Mongoose, because the type must be equal to one of the following primitives: String, Number, Boolean, Array, Buffer, Date, ObjectId, Mixed. Also, please note that property: [mongoose.Schema.ObjectId] is equal to property: [{type: mongoose.Schema.ObjectId}]. See also Mongoose SchemaTypes documentation for more examples.
Sorry, I marked this as the accepted answer before I had chance to test it. It seems like it doesn't make any difference. See above.
This is the approach I'm using and works fine under Mongoose 3.8.8, what is your problem with it??
The problem is that the positions array isn't populated, it just returns an array of ids.
3
+25

Try to use "mongoose.Schema.Types.ObjectId" instead of "mongoose.Schema.ObjectId", I had a similar issue some time ago. But I am sure not whether it helps in your case.

Comments

1

I've got no further forwards with this.

I've restructured everything so that I can query the subdocuments directly rather than relying on the populate method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.