1

I'm trying to copy an existing collection to a new collection within the same database, emulating the behavior of running db.source.aggregate({"$out":"target"}); in the shell.

On older versions of mongodb and the java driver, it was possible to do (as seen here):

// set up pipeline
List<DBObject> ops = new ArrayList<DBObject>();
ops.add(new BasicDBObject("$out", "target")); // writes to collection "target"

// run it
MongoClient client = new MongoClient("host");
DBCollection source = client.getDB("db").getCollection("source")
source.aggregate(ops);

but since mongo 3.0.0, the writers are moving away from DBCollection and onto MongoCollection and others that don't have the same functionalities, specifically for .aggregate(List<DBObject>).

I've tried the following options, none of which had any effect:

List<Bson> ops = new ArrayList<>();
ops.add(new BasicDBObject("$out", "target"));
//OR
ops.add(new Document("$out", "target")); //not at the same time as above

MongoClient client = new MongoClient("host");
MongoCollection source = client.getDatabase("db").getCollection("source");
source.aggregate(ops);

Sadly, I don't understand aggregate operations well enough to figure this out.

Is there any similar way to do this with the java driver and mongo 3.4?
Are there any other ways that'll cause the copy serverside?

Thanks

Community
  • 1
  • 1
Joey Baruch
  • 4,180
  • 6
  • 34
  • 48

3 Answers3

3

You can try this:

MongoClient client = new MongoClient("host");
MongoCollection source = client.getDatabase("db").getCollection("source");    
source.aggregate(Arrays.asList(out("<OutputcollectionName>")));

use the following import statement:

 import static com.mongodb.client.model.Aggregates.*;
JayKrish
  • 937
  • 6
  • 13
  • I'm still not seeing any changes in the target collection, if i don't create the target collection before, i don't even see an empty collection with that name in the command line, and `client.getDatabase("db").getCollection("target").count()` returns 0. **could this be a configuration issue?, is there something you need to do to enable aggregation ops?** – Joey Baruch Feb 16 '17 at 08:41
  • No configuration is needed. Aggregation works out of the box. Just check if the connection and db collection are correct by running a find query and printing the output. It works on my machine as is. – JayKrish Feb 16 '17 at 08:50
  • 2
    thanks for the quick response :). For some reason, if i run `source.aggregate(Arrays.asList(out("target"))).foreach(noOp);` where `noOP` is a `Block` that does nothing, similar to the printBlock [found in the documentaton](http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/aggregation/), then it works fine, but if i leave it as is, then it does nothing. – Joey Baruch Feb 16 '17 at 09:52
2

Add .toCollection() .. that should work without noOps block

Supreet ST
  • 21
  • 1
-1
source.aggregate(Arrays.asList(out("<OutputcollectionName>"))).toCollection();
Cow
  • 2,543
  • 4
  • 13
  • 25
Xushipeng
  • 1
  • 1