2015-02-06 54 views
0

我對貓鼬和mongo比較新。我設計兩個模式順便說一句,我使用的NodeJS新來的貓鼬,架構dbref使用字符串類型,但填充方法不起作用

設備

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

我試圖做的話,可以巢設備對象使用貓鼬填充功能進入設備關係,如此視頻所示

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" 
    }, 

... 
] 

請幫我!謝謝!

回答

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

我試過這種方法。它不起作用 – swfong 2015-02-07 05:05:21