I have a pipeline that seems to work fine in Java until I try to write it to a collection. I thought I could simply add
pipeline.add(new Document("$merge", “testCollection));
this doesn’t seem to create the collection. I have tried
pipeline.add(new Document(”$merge", new Document(“into”,testCollection)));
but that doesn’t work either, nor does $out.
When you say “it doesn’t work” what happens? Do you get an error? What’s the version or server/driver?
Are you authenticated as a user that can write to the database?
I still seem to have issues with merge.
It was working with pipeline.add(new Document("$out", “testCollection"));
I changed a few things including changing that to pipeline.add(new Document("$merge", “testCollection"));
that threw an exception with the error message "Value expected to be of type DOCUMENT is of unexpected type STRING"
According to the manual { $merge: <collection> } is an acceptable simplified form, but I get that exception. I don’t get an exception from pipeline.add(new Document("$merge", new Document("into", "testCollection"))); but it doesn’t create new collection.
There is also a second parameter that you can use to pass merge options if you want any.
It looks like you have issues with curly double quotes as well in your code. I’m wondering if the issue couldn’t come from that.
Also, if you really don’t want to collect the result, I would use .first() maybe instead of .into() to avoid creating a useless list and save some memory.
I believe the aggregation helper has some limitations ($lookup) which will affect the rest of the pipeline. I’m not sure if I can mix and match and if I can, it won’t help with the readability.
I guess I’d better have a play and see if I can get that to work.
I don’t seem to have curly quotes in the original code. I’m not sure where they came from. If they were in the code I was running, I would not expect it to compile.
OK, I was looking in the wrong place. The source collection name had been changed accidentally, so I was no longer getting any data and merge was refusing to create an empty collection. I thought the issue was the merge stage as I hadn’t intentionally changed the source name.