2017-07-14 68 views
0

我有一個收藏關聯問題。
我花了2天,仍然沒有解決問題,這對我來說是新的。
收藏夾嵌套在貓鼬

我的模型:

// Schema opened cases 
const openedSchema = new Schema({ 
    user: { 
    type: Schema.Types.ObjectId, 
    ref: 'user', 
    required: [true, 'user is required'], 
    index: true 
    }, 
    weapon: { 
    type: Schema.Types.ObjectId, 
    ref: 'cases.weapons', 
    required: [true, 'weapon is required'], 
    index: true 
    }, 
    sellPrice: { 
    type: Number, 
    default: null 
    }, 
    status: { 
    type: Number, 
    default: 0 
    } 
}, { 
    timestamps: true 
}); 

const opened = mongoose.model('opened', openedSchema); 

// list cases 
const casesSchema = new Schema({ 
    name: { 
    type: String, 
    unique: true, 
    required: [true, 'name is required'] 
    }, 
    price: { 
    type: Number, 
    required: [true, 'price is required'] 
    }, 
    weapons: [ { 
    weapon: { 
     type: Schema.Types.ObjectId, 
     ref: 'weapon', 
     index: true 
    } 
    } ] 
}, { 
    timestamps: false 
}); 

const cases = mongoose.model('cases', casesSchema); 

// list weapons 

const weaponSchema = new Schema({ 
    name: { 
    type: String, 
    unique: true, 
    required: [true, 'name is required'] 
    }, 
    price: { 
    type: Number, 
    required: [true, 'price is required'] 
    }, 
    autoship: { 
    count: Number, 
    status: Boolean, 
    price: Number 
    } 
}, { 
    timestamps: false 
}); 

const weapon = mongoose.model('weapon', weaponSchema); 

這文件是什麼樣子

// cases 
{ 
    "_id": { 
     "$oid": "59653bcfa9ac622e1913e10c" 
    }, 
    "name": "test case #1", 
    "price": 256, 
    "weapons": [ 
     { 
      "weapon": { 
       "$oid": "59653bcfa9ac622e1913e10b" 
      }, 
      "_id": { 
       "$oid": "59653bcfa9ac622e1913e10d" 
      } 
     }, 
     { 
      "_id": { 
       "$oid": "59653d3279aeda2fda9fb490" 
      }, 
      "weapon": { 
       "$oid": "59653c5d069f562eb0ba4ef3" 
      } 
     }, 
     { 
      "_id": { 
       "$oid": "59653d38ba04de2fdddc459f" 
      }, 
      "weapon": { 
       "$oid": "59653c893a772e2ef7b65a29" 
      } 
     } 
    ], 
    "__v": 0 
} 
// opened 
{ 
    "_id": { 
     "$oid": "5965d134c8c95972a1a498f5" 
    }, 
    "updatedAt": { 
     "$date": "2017-07-12T07:35:16.419Z" 
    }, 
    "createdAt": { 
     "$date": "2017-07-12T07:35:16.419Z" 
    }, 
    "user": { 
     "$oid": "5965d0d6ea9db872360db98b" 
    }, 
    "weapon": { 
     "$oid": "59653bcfa9ac622e1913e10d" 
    }, 
    "status": 0, 
    "sellPrice": null, 
    "__v": 0 
} 
// weapon 
{ 
    "_id": { 
     "$oid": "59653bcfa9ac622e1913e10b" 
    }, 
    "name": "AWP | Fever Dream", 
    "price": 300, 
    "autoship": { 
     "status": true, 
     "price": 167, 
     "count": 5 
    }, 
    "__v": 0 
} 

我需要的開宗武器數據的列表。
打開 - >經典案例 - >武器
所以,我這樣做:

opened.find() 
.populate('cases.weapons') 
    .then(_opened => { 
     console.log(_opened); 
    }) 
    .catch(err => { 
     logger.error(err); 
    }); 

populate不起作用。

回答

0

除非我誤會,openedSchemacasesSchema之間沒有關係。

這不是opened -> cases -> weaponopened -> weaponopenedSchema沒有現場叫cases - 這意味着cases將永遠不會被填充。

根據您的架構定義,它應該是opened.find().populate('weapon')

+0

每個案例都有一個武器列表和它的id爲'populate'。 'opens.weapon'的'cases.weapons._id'和'weapon'的'cases.weapons.weapon._id'。這我想實現。如果我在某個地方犯了錯,我請求你糾正我的方案 –