2016-10-07 14 views
0

私はモデルを持っている:ノードMongoのクエリ巣オブジェクト/モデル_id

var carSchema = new mongoose.Schema({ 
    year: { 
     type: [Number], 
     required: true 
    }, 
brand: { 
    type: String, 
    required: true 
}, 
model: { 
    type: String, 
    required: true 
} 

}); 

var productSchema = new mongoose.Schema ({ 
    name: { type: String, required: true }, 
    // Pictures must start with "http://" 
    pictures: [{ type: String, match: /^http:\/\//i }], 
    price: { 
    amount: { 
     type: Number, 
     required: true 
    }, 
    // Only 2 supported currencies for now 
    currency: { 
     type: String, 
     enum: ['CAD','USD'], 
     required: true, 

    } 
    }, 
    category: Category.schema, 
    cars: [Car.schema], 
    internal: { 
    approximatePriceCAD: Number 
    } 
}); 

私は、ネストされた車の_idを照会しようとしているが、それは私にすべての文書を返していません。モデルやブランドなどの他の車のパラメータを照会すると結果は表示されますが、_idは表示されません。例えば

app.get('/product/vehicle/:id', function(req, res) { 
    Product.find({'cars._id' : req.params.id}, function(error, docs) { 
     return res.json({products: docs}); 
    }); 
}); 

は、空のドキュメントを返します。

app.get('/product/vehicle/:brand', function(req, res) { 
    Product.find({'cars.brand' : req.params.brand}, function(error, docs) { 
     return res.json({products: docs}); 
    }); 
}); 

私に適切な文書が返されます。何か案は?

答えて

0

さて、

テストの問題は、製品のモックオブジェクトは、車のデシベル挿入前に作成されたということでした。したがって、_idは、製品が車に関して作成され、格納されたときに異なっていた。それがどのように照会されるかには何も問題はありません。