I want to use my array field 0th value to find a match in sale document using Mongo aggregate $lookup query. Here is my query:
db.products.aggregate([
{
$match : { _id:ObjectId("57c6957fb190ecc02e8b456b") }
},
{
$lookup : {
from : 'sale',
localField: 'categories.0',
foreignField: 'saleCategoryId',
as : 'pcSales'
}
}]);
Result :
{
"_id" : ObjectId("57c6957fb190ecc02e8b456b"),
"categories" : [
"57c54f0db190ec430d8b4571"
],
"pcSales" : [
{
"_id" : ObjectId("57c7df5f30fb6eacb3810d1b"),
"Title" : "Latest Arrivals",
}
]}
This query will return me a match but when i check it not a match. I don't get why is this happening, And when i removed 0th part from query its return blank array. Like this:
{
"_id" : ObjectId("57c6957fb190ecc02e8b456b"),
"categories" : [
"57c54f0db190ec430d8b4571"
],
"pcSales" : []
}
saleCategoryId is also a array field which contain array of categoriesKey.
Please help.