Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregation $project, $unwind, $lookup returns EMPTY result, but works in a Java main() #372

Open
mehtacuk opened this issue Jun 12, 2019 · 0 comments

Comments

@mehtacuk
Copy link

Any solution for the above issue?

I have a similar issue here

My query on Mongo Shell is like -

db.getCollection('cm').aggregate([
{ "$match": { "ID": "2006"} },
{
"$lookup": {
"from": "cm",
"localField": "Parent",
"foreignField": "_id",
"as": "bList"
}
},
{"$unwind":"$bList"},
{
"$lookup": {
"from": "cm",
"localField": "bList.Parent",
"foreignField": "_id",
"as": "bList1"
}
},
{"$unwind":"$bList1"},
{
$project: {
"_id": 0,
"DivisionId": "$bList.Parent",
"GroupID": "$bList._id",
"GroupName": "$bList.Name",
"DivisionName":"$bList1.Name"
}
}
])

JSON document 1: -
{ "_id" : "ProductGroup2006", "Parent" : "Group6", "Ancestors" : [ "Division2", "Group6" ], "ID" : "2006" }

JSON document 2:
{ "_id" : "Division2", "Parent" : "", "Ancestors" : [], "Name" : "Sujay Godbole", "Type" : "Division" }

JSON Document 3
{ "_id" : "Group6", "Parent" : "Division2", "ID" : "6", "Ancestors" : [ "Division2" ], "Name" : "Chirag Mehta" }

After adding all these documents to the Collection in JUNIT class, call the below:

`List aggregation = new ArrayList();
aggregation.add(Aggregates.match(Filters.eq("ID", "2006")));
aggregation.add(Aggregates.lookup("cm", "Parent", "_id", "siblingList"));
aggregation.add(Aggregates.unwind("$siblingList"));
aggregation.add(Aggregates.lookup("cm", "siblingList.Parent", "_id", "ancestorList"));
aggregation.add(Aggregates.unwind("$ancestorList"));

	aggregation.add(Aggregates.project(
			new Document("_id", 0).append("DivisionId", "$siblingList.Parent").append("GroupId", "$siblingList._id")
					.append("GroupName", "$siblingList.Name").append("DivisionName", "$ancestorList.Name")));`

Finally -
AggregateIterable<Document> iterable = mongoCollection.aggregate(aggregate);

And this action results in EMPTY objects

ISSUE: My main() works and returns results; but the same in Fongo - collection.aggregate(), return EMPTY results.

Any help/resolution appreciated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant