I use Mongoose, Express and Node.
I have my four schemas:
Step.js
var StepSchema = new mongoose.Schema({ ... });
module.exports = StepSchema;
TC.js
var Step = require('Step');
var TCSchema = new mongoose.Schema({ stepList: [Step] });
module.exports = TCSchema;
TS.js
var TC = require('TC');
var TSSchema = new mongoose.Schema({ tcList: [TC] });
module.exports = TSSchema;
TR.js
var TS = require('TS');
var TRSchema = new mongoose.Schema({ tsList: [TS] });
module.exports = mongoose.model('TR', TRSchema);
Example Data:
{
_id: ObjectId("32d33ddf54de3")
tsList: [
{
_id: ObjectId("66d4f66d44f88")
tcList: [
{
_id: ObjectId("8df84ff8fssdeg")
stepList: [
{
_id: ObjectId("5484kkfci393d")
}
]
}
]
}
]
}
Now I would like to retrieve my tcList using the id like this for ex:
http://localhost:3000/api/tc/8df84ff8fssdeg
The expected output is:
{
_id: ObjectId("8df84ff8fssdeg")
stepList: [
{
_id: ObjectId("5484kkfci393d")
}
]
}
I'm totally not aware on how to build my mongoose's find
here. I'm thinking that Aggregate
may work, but unsure on how to use Aggregate
. I need your help.
Update
I've tried the following as suggested by @hassansin, but on console it prints empty array and the response of webservice is replying back with No response received:
var gettcList = function(tcId) {
TR.aggregate(
[
// match by tcList _id and reduce the no of documents for the next pipeline stages
{
$match: {
"tsList.tcList._id": mongoose.Types.ObjectId(tcId)
}
},
// deconstruct tsList array
{
$unwind: "$tsList"
},
// deconstruct tcList array
{
$unwind: "$tsList.tcList"
},
// match again in the deconstructed document list
{
$match: {
"tsList.tcList._id": mongoose.Types.ObjectId(tcId)
}
},
// project the fields
{
$project: {
_id: "$tsList.tcList._id",
stepList: "$tsList.tcList.stepList"
}
}
], function(err, result) {
if (err) {
console.log(err);
return next(err);
}
console.log(result);
return result;
}
);
};
And the web service part is:
router.get('/tc/:tcid', function(req, res, next) {
res.json(gettcList(req.params.tcid));
});