新的mongoose,Schema dbref使用字符串类型但populate方法不起作用

时间:2015-02-06 11:27:46

标签: node.js mongodb mongoose

我对mongoose和mongo比较新。我正在设计两个模式 btw,我正在使用nodeJS

设备

var deviceSchema = new Schema({
    device_id : {type: String}
    , name : {type: String, default: null}
    , code : {type: String, default: null}
    , status : {type: String, default: null}
    , type : {type: String, default: null}
    , type2 : {type: String, default: null}
    , description : {type: String, default: null}
    , deployment_date : {type: Date, default: null}
    , create_date : {type: Date, default: null}
    , last_update : {type: Date, default: null}
    , entity_id : {type: String, ref: 'entity'}
    , enterprise_id : {type: String, ref: 'enterprise'}
});

设备关系

var deviceRelationshipSchema = new Schema({
    device_relationship_id : {type: String}
    , device_id : {type: String, ref: 'device'}
    , entity_id : {type: String, ref: 'entity'}
    , last_update : {type: Date, default: null}
    , create_date : {type: Date, default: null}
    , app_name : {type: String, default: null}
    , authorize : {type: String, default: null}
});

我正在尝试使用moongoose populate功能将设备对象嵌套到设备关系中,如本视频所示

https://www.youtube.com/watch?v=5e1NEdfs4is

这是方法

var mongoose = require('mongoose');
mongoose.model('device_relationship').find({}, function(err, deviceRelationship){
        mongoose.model('device_relationship').populate(deviceRelationship, { path : 'device' }, function (err, deviceRelationship){
            res.json(deviceRelationship);
        })
    });

但是,我的结果仍然没有嵌套在其中的设备对象

结果

[
  {
    "_id": "54d3248516d64ae206752a75",
    "device_relationship_id": "VZ25VRVA-3FOIA8RM-N5ABI98A",
    "device_id": "z0a783118008",
    "__v": 0,
    "authorize": null,
    "app_name": null,
    "create_date": "2014-05-01T10:00:00.000Z",
    "last_update": "2015-02-05T08:06:29.000Z"
  },

...
]

请帮帮我!谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用exec。

var mongoose = require('mongoose');
var device_relationship = mongoose.model('device_relationship');
device_relationship.find({}).populate('device').exec(function(err, deviceRelationship){
    res.json(deviceRelationship);
});