Tying to get a lookup working with the async java driver to join 2 collections. But I can't figure out the correct syntax to use as I can't find any documentation on what the syntax is using the Let (variables) version of the $lookup command is to work properly.
Here is what using (and it doesn't find any matches):
final List<Bson> lookUppipeline = new ArrayList<>();
final List<Variable<?>> variables = Arrays.asList(new Variable<>("id", "$_id"));
lookUppipeline.add(match(eq("object_id", "$$id")));
final Bson lookup = lookup("values_collection", variables, lookUppipeline, "tag_values");
final AggregateIterable<ApiJsonObject> findIter = info_collection.aggregate(Arrays.asList(lookup, skip(0), limit(1_000), project(exclude(EXCLUDES_LIST))));
Basically the $_id
in the info_collection
is equal to the object_id
of the values_collection
.
Do I need to create the Variable differently or the match equals? If I use the foreign key version of the lookup method it works but I need to use pipelines as I have other things to add to it.
There are examples using the sync driver but not with the async driver.
The syntax from a mongodb query is
db.info_collection.aggregate([
{
$lookup:{
from: "values_collection",
let: { id: "$_id" },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$object_id", "$$id" ] }
]
}
}
}
],
as: "tag_values"
}
}
])
Here is some sample data (trimmed down but all relevant):
info_collection
:
[
{"_id":"cce9ec95-dd03-4066-89c5-86227be70503", "Name":"Object1" }
{"_id":"cce9ec95-dd03-4066-89c5-86227be70503", "Name":"Object2" }
{"_id":"cce9ec95-dd03-4066-89c5-86227be70503", "Name":"Object3" }
{"_id":"cce9ec95-dd03-4066-89c5-86227be70503", "Name":"Object4" }
{"_id":"cce9ec95-dd03-4066-89c5-86227be70503", "Name":"Object5" }
]
values_collection
:
[
{"object":"cce9ec95-dd03-4066-89c5-86227be70503", "_id":{"$oid":"5e1cee26610b668017537cc2"}, "Value":"test1", "Tag":1},
{"object":"6494bcec-c94f-4421-9f5a-84a76edda8fd", "_id":{"$oid":"5e1cee26610b668017537cc4"}, "Value":"test2", "Tag":1},
{"object":"ea40aaf7-1d7c-4bf2-8a98-93cfbf62035d", "_id":{"$oid":"5e1cee26610b668017537cc6"}, "Value":"test3", "Tag":1},
{"object":"7556a86d-4962-4220-8a77-10655e8e4376", "_id":{"$oid":"5e1cee26610b668017537cc8"}, "Value":"test4", "Tag":1},
{"object":"f78d4302-0756-47bb-aaff-c93744d147fe", "_id":{"$oid":"5e1cee26610b668017537cca"}, "Value":"test5", "Tag":1},
{"object":"06ade084-3e2a-42eb-9063-5059c447e518", "_id":{"$oid":"5e1cee26610b668017537ccc"}, "Value":"test6", "Tag":1}
]