Using MongoDB shell I use:
db.bios.aggregate(
[
{$match:{"contribs.0.name":{"$exists":1}}},
{$project: {contribs:{$arrayElemAt:["$contribs",0]}}}
]
)
How can I make the same query using Java driver (2.14.1)? I try with:
At first I create a DBObject for $match stage:
DBObject match = new BasicDBObject("$match",new BasicDBObject("contribs.0.name",
new BasicDBObject("$exists",1)));
Then I create a BasicDBList:
BasicDBObject obj = new BasicDBObject("$contribs",0);
BasicDBList arrayElemAt = new BasicDBList();
arrayElemAt.add(obj);
And this is the $project stage:
DBObject project1 = new BasicDBObject("$project", new BasicDBObject("contribs",
new BasicDBObject("$arrayElemAt",arrayElemAt)));
Finally I create the aggregation pipeline:
List<DBObject> list = new ArrayList<>();
list.add(match);
list.add(project1);
AggregationOutput output = this.coll.aggregate(list);
$Match stage works, but $project does not.
I get an error: "errmsg" : "invalid operator '$contribs'" , "code" : 15999