1
var CarSchema = new Schema({
name: {type: String},
partIds: [{type: Schema.Types.ObjectId, ref: 'Part'}],
});
var PartSchema = new Schema({
name: {type: String},
props: [
{ colour: {type: String}, shape: {type:String} }
],
});
Car = {
name: "BMW",
partIds:[ObjectId("57baa43e152654f80aac36a6")]}
Part = {
_id: ObjectId("57baa43e152654f80aac36a6"),
name: "Piston",
props: [{colour:"red", shape: "Cubical"},{colour:"green", shape: "cylindrical"}]
所以,當我詢問我應該得到這樣的文檔:
Car = {
name: "BMW",
partIds: [{
_id:ObjectId("57baa43e152654f80aac36a6"), name:"Piston", props: [{colour:"red", shape:"cubical"}]
}
道具陣列應該只有元素顏色爲紅色
我想用部件數組填充汽車,使其prop數組只有對象具有colo你的紅色。有沒有辦法做到這一點,或者我將不得不採取老式的方式,並通過道具陣列使其顏色與紅色相匹配。
你已經有'零件'集合?或者你創建它並將它的ID存儲在'car'集合中? –
零件集合是不同的,它的ID存儲在汽車集合的partIds –
,所以如果顏色是紅色的,你想創建'part'文件並將它的id存儲在'car'集合中? –