Support new aggregation array operators for Java Driver (3.2)

796 views
Skip to first unread message

Yaroslav Starikovich

unread,
Jan 23, 2016, 5:27:45 PM1/23/16
to mongodb-user
Hi All,

I have a question. Mongodb 3.2 provides a new features to operate arrays and this approach works more faster. This new approach really fast

db.test.aggregate([
    { $match: {'test.id': {$in: [7011,5812]}}},
    { $project: {
        test: { $filter: {
            input: '$test',
            as: 'item',
            cond: { $or: [
              { $eq: ['$$item.id', 7011]},
              { $eq: ['$$item.id', 5812]}
             ]}
        }}
    }}
])

And I would like remove old one

db.customers.aggregate(
    { $match: {'mccStatistics.id': {$in: [7011,5812]}}},
    { $unwind: '$mccStatistics'},
    { $match: {'mccStatistics.id': {$in: [7011,5812]}}},
    { $group: {_id: '$_id', mccStatistics: {$push: '$mccStatistics'}}}
)

But I didn't see support for projection filter in the driver. So when do you plan introduce this feature for Java driver?

Hakan

unread,
Jan 24, 2016, 5:25:25 AM1/24/16
to mongodb-user
You can use the Document.parse static method for your aggregation query using the Java driver as in the following example:

pipeline = Arrays.asList(
    Document.parse({" $match: {'mccStatistics.id': {$in: [7011,5812]}}}"),
    Document.parse({" $unwind: '$mccStatistics'}"),
    Document.parse({" $match: {'mccStatistics.id': {$in: [7011,5812]}}}"),
    Document.parse({" $group: {_id: '$_id', mccStatistics: {$push: '$mccStatistics'}}}")
);

And there is a class for projection in the driver

import com.mongodb.client.model.Projections;

Projections.exclude("_id")

Alireza Mohamadi

unread,
Feb 18, 2017, 2:25:20 PM2/18/17
to mongodb-user
@Hakan thanks it's a nice procedure, but doesn't eliminate requirement for $filter stage inside the driver.
Also good to mention, comparison operators have different syntax when being used inside an aggregation stage (e.g. $gt which takes an array as second parameter) and driver developers should consider adding them into the driver.

Wan Bachtiar

unread,
Feb 27, 2017, 12:29:27 AM2/27/17
to mongodb-user

Mongodb 3.2 provides a new features to operate arrays and this approach works more faster. This new approach really fast

As hinted by Hakan, you could parse $filter through Document class. Using your test collection example see below:

Document match = Document.parse("{ $match: {'test.id': {$in:[7011, 5812] } }}");

Document project = Document.parse("{ $project: { test: { $filter: { input: '$test', as:'item', cond:{$or: [{$eq:['$$item.id', 7011]}, {$eq:['$$item.id', 5812]}]}}}}} ");

List<Document> pipeline = asList(match, project);

AggregateIterable<Document> iterable = db.getCollection("test").aggregate(pipeline);

The above snippet is tested on MongoDB Java Driver v3.2.2 and v3.4.2. Aggregation operator $filter is available on MongoDB v3.2+.

but doesn’t eliminate requirement for $filter stage inside the driver.

Currently MongoDB Java Driver provides typed builders only for aggregation stages and accumulators, but not the full expression language (which $filter is part of ). There is currently an open tracking ticket for this support JAVA-1949. Please feel free to up-vote or add yourself as a watcher for updates on the ticket.

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages