1
我試圖將現有集合複製到同一個數據庫中的新集合,模擬在shell中運行db.source.aggregate({"$out":"target"});
的行爲。如何使用java驅動程序複製mongodb集合(v3.4)
在舊版本的MongoDB和Java驅動程序,這是可以做到的(如圖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);
但由於蒙戈3.0.0,the writers are moving away fromDBCollection
和到MongoCollection
和其他人不具有相同的功能,特別是.aggregate(List<DBObject>)
。
我試過下面的選項,其中沒有任何效果:
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);
可悲的是,我不明白,總操作不夠好圖了這一點。
有沒有類似的方式來做到這一點與java驅動程序和mongo 3.4?
是否有任何其他方式會導致複製服務器端?
感謝
我仍然沒有看到目標集合中的任何改變,如果我不」 t之前創建目標集合,我甚至沒有在命令行中看到具有該名稱的空集合,並且'client.getDatabase(「db」).getCollection(「target」)。count()'返回0。 *這可能是一個配置我ssue?,是否有需要做的事情來啓用聚合操作?** –
無需配置。聚合可以直接使用。只需通過運行查找查詢並打印輸出來檢查連接和db收集是否正確。它在我的機器上工作。 – JayKrish
感謝您的快速回復:)。出於某種原因,如果我運行'source.aggregate(Arrays.asList(out(「target」)))。foreach(noOp);'其中'noOP'是一個'Block',它什麼都不做,類似於printBlock [發現在文檔](http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/aggregation/),那麼它工作正常,但如果我離開它,那麼它什麼都不做。 –