填充数组类型嵌套对象时出现Mongoose - .populate问题

时间:2014-07-10 03:08:08

标签: javascript node.js mongodb mongoose

我正在尝试填充对象myObj。将值保存到'a'对象后,我能够在db中看到'myObj'的数据。但是当我查询obj'a'时,它没有拉出'myObj'数据。我的架构如下。

var a= new Schema({
    b: 'Date',
    c: {
        d: {
            type: Schema.ObjectId,
            ref: 'Student'
        },
        myObj: {
            type: [{
                type: Schema.ObjectId,
                ref: 'Exam'
            }],
            select: true
        },
    }
});

考试模式

var ExamSchema= new Schema({
    name: String,        
    subject: {
        type: [{
            name: String, 
            marks: Number            
        }],
        select: false
    }
});

我的查询方法:

exports.a = function(req, res, next, id) {
    A //model of 'a'
        .findOne({
            _id: id
        })
        .populate('c.d c.myObj')
        .exec(function(err, aObj) {
            if (err) return next(err);
            if (!aObj) return next(new Error('Failed to load  ' + id));            
            req.a = aObj;
            next();
        });
};

预期输出:

var a = new Schema({
    b: "2014-07-10T02:30:00.005Z",
    ,
    c: {
        d: {
            name: 'Hari',
            age: 10
        },
        myObj: {
            [{
                name: 'Quaterly',
                subject: {
                    [{
                        name: 'English',
                        marks: 100
                    }, {
                        name: 'Maths',
                        marks: 90
                    }, {
                        name: 'Science',
                        marks: 100
                    }],
                    select: false
                }
            }, {
                name: 'Half-Yearly',
                subject: {
                    [{
                        name: 'English',
                        marks: 100
                    }, {
                        name: 'Maths',
                        marks: 90
                    }, {
                        name: 'Science',
                        marks: 100
                    }],
                    select: false
                }
            }],
            select: true
        },
    }
});

**Actual Output:**

    var a = new Schema({
        b: "2014-07-10T02:30:00.005Z",
        ,
        c: {
            d: {
                name: 'Hari',
                age: 10
            },
            myObj: []
        }
    });

请告诉我错误的地方。感谢。

1 个答案:

答案 0 :(得分:2)

这应该有效,这是一个减少的样本,用于测试和比较,看看你实际上可能做的不同:

var async = require('async'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/throw');

var oneSchema = new Schema({
  name: String
});

var twoSchema = new Schema({
  name: String
});

var testSchema = new Schema({
  name: String,
  c: {
    d: { type: Schema.Types.ObjectId, ref: "One" },
    e: [{ type: Schema.Types.ObjectId, ref: "Two" }]
  }
});

var Test = mongoose.model( "Test", testSchema, "test" );
var One =  mongoose.model( "One", oneSchema, 'one' );
var Two =  mongoose.model( "Two", twoSchema, 'two' );


var test = new Test({ name: "test" });

var one = new One({ name: "one" });

var two = new Two({ name: "two" });
var three = new Two({ name: "three" });

test.c.d = one;
test.c.e.push(two,three);

async.series([

  // Remove prior
  function(callback) {
    async.each([Test,One,Two],function(model,complete) {
      model.remove(function(err) {
        if (err) throw err;
        complete();
      });
    },function(err) {
      if (err) throw err;
      callback();
    });
  },

  // Save new
  function(callback) {
    async.each([test,one,two,three],function(model,complete) {
      model.save(function(err) {
        if (err) throw err;
        complete();
      });
    },function(err) {
      if (err) throw err;
      callback();
    });
  },


  // Unpopulated
  function(callback) {
    Test.findOne()
      .exec(function(err,obj) {
        if (err) throw err;

        console.log( "Before: %s", JSON.stringify( obj, undefined, 4 ) );
        callback();
      });
  },

  // Populated

  function(callback) {
    Test.findOne()
      .populate("c.d c.e")
      .exec(function(err,obj) {
        if (err) throw err;

        console.log( "After: %s", JSON.stringify( obj, undefined, 4 ) );
        callback();
      });
  }

],function(err) {
  if (err) throw err;
  process.exit();
});

哪个应该产生以下结果:

Before: {
    "_id": "53be11986c64035664e4d73a",
    "name": "test",
    "__v": 0,
    "c": {
        "d": "53be11986c64035664e4d73b",
        "e": [
            "53be11986c64035664e4d73c",
            "53be11986c64035664e4d73d"
        ]
    }
}
After: {
    "_id": "53be11986c64035664e4d73a",
    "name": "test",
    "__v": 0,
    "c": {
        "d": {
            "_id": "53be11986c64035664e4d73b",
            "name": "one",
            "__v": 0
        },
        "e": [
            {
                "_id": "53be11986c64035664e4d73c",
                "name": "two",
                "__v": 0
            },
            {
                "_id": "53be11986c64035664e4d73d",
                "name": "three",
                "__v": 0
            }
        ]
    }
}