I'm not able to reproduce this using the 3.1.0 Java driver.
Given the following dataset (using shell syntax):
{ "_id" : ObjectId("561ebe3eabe46b29fbeb8749"), "data" : { "link" : [ { "somedata" : 1 }, { "somedata" : 2 } ] } }
{ "_id" : ObjectId("561ebe88abe46b29fbeb874a"), "data" : { "link" : [ { "somedata" : 1 }, { "somedata" : "something" } ] } }
{ "_id" : ObjectId("561ebe89abe46b29fbeb874b"), "data" : { "link" : [ { "somedata" : 1 }, { "somedata" : "something" } ] } }
and the following Java program:
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.json.JsonMode;
import org.bson.json.JsonWriterSettings;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Aggregates.unwind;
import static com.mongodb.client.model.Filters.eq;
import static java.util.Arrays.asList;
public class AggregatesTest {
public static void main(String[] args) {
Bson unwind = unwind("$data.link");
Bson match = match(eq("data.link.somedata", "something"));
List<Bson> pipeline = asList(unwind, match);
MongoClient c = new MongoClient();
MongoCollection<Document> collection = c.getDatabase("arrays").getCollection("mydata");
List<Document> results = collection.aggregate(pipeline).into(new ArrayList<Document>());
results.forEach(document -> System.out.println(document.toJson(new JsonWriterSettings(JsonMode.SHELL))));
}
}
I get the following output:
{ "_id" : ObjectId("561ebe88abe46b29fbeb874a"), "data" : { "link" : { "somedata" : "something" } } }
{ "_id" : ObjectId("561ebe89abe46b29fbeb874b"), "data" : { "link" : { "somedata" : "something" } } }
Is it possible that some of the documents in your collection have data.link values that are not arrays?
Regards,