简体   繁体   English

在MongooseJS中填充子文档数组

[英]Populate array of sub-documents in MongooseJS

I'm having a hard time trying to populate an array of sub-documents with MongooseJS. 我很难用MongooseJS填充子文档数组。 Could someone help? 有人可以帮忙吗?

Here is user.js: 这是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 这是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. 调用getUserByID时,不会填充用户架构的positions数组。 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. 我还从位置模式中删除了location属性,以防万一。

Nothing I've tried seems to make any difference at all. 我尝试过的一切似乎都没有任何不同。

Here is what is currently saved in the users collection: 这是当前保存在users集合中的内容:

{
    "__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: 这是positions集合中的内容:

{
    "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. 作为参考,我在package.json文件中使用了"mongoose": "^3.8.8"

I'm really stumped by this, any help would be appreciated. 我为此感到非常难过,任何帮助将不胜感激。

You made a small but important typo in your user schema - the type of position field should not be set to array of ObjectId . 您在用户模式中做了一个小而重要的错字- position字段的类型不应设置为ObjectId数组。 What you probably wanted is this schema : 您可能想要的是这个模式:

var userSchema = new mongoose.Schema({
    name: {
        type: String
    },
    positions: [{
        type: mongoose.Schema.ObjectId,
        ref: 'position'
    }]
});

Try to use "mongoose.Schema.Types.ObjectId" instead of "mongoose.Schema.ObjectId", I had a similar issue some time ago. 尝试使用“ mongoose.Schema.Types.ObjectId”代替“ mongoose.Schema.ObjectId”,前一段时间我遇到了类似的问题。 But I am sure not whether it helps in your case. 但是我不确定这是否对您有帮助。

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. 我已经对所有内容进行了重组,以便可以直接查询子文档,而不必依赖于填充方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM