I am using mongoose for connecting mongodb in node.js, now i have a document schema as given below
var ArraySchema = new Schema({
array: [{type: String}],
counter: {type: 'Number', required: true}
});
Now i want to fetch array element whose position is counter which is present in the document as well, i read many questions like this on SO and on most of them i found mongoose aggregation but i don't know how to use aggregation to solve my problem.
If anyone of you have used aggregation please help me.
Rohail Najam :
Use this query in my mongoose. \n\nvar aggregation = [\n{\n $project : {\n array : {$arrayElemAt: [ \"$array\", \"$counter\" ] } \n }\n}]\ndb.collectionName.aggregate(aggregation).exec(function(err, model){\nif(err){\n// handle error}\nconsole.log(model);\n})\n",
2016-11-07T16:55:12