ご質問がある場合:
上記の文字列をJavaドライバに渡してドライバで実行させることはできますか?
それからはdb.evalコマンドを使用することができます。たとえば:
MongoDatabase database = mongoClient.getDatabase("...");
Bson command = new Document("eval", "db.orders.aggregate([\n" +
" {\n" +
" $unwind: \"$specs\"\n" +
" },\n" +
" {\n" +
" $lookup:\n" +
" {\n" +
" from: \"inventory\",\n" +
" localField: \"specs\",\n" +
" foreignField: \"size\",\n" +
" as: \"inventory_docs\"\n" +
" }\n" +
" },\n" +
" {\n" +
" $match: { \"inventory_docs\": { $ne: [] } }\n" +
" }\n" +
"])");
Document result = database.runCommand(command);
しかし ... db.eval
コマンドは非推奨とその使用is not advisedされます。 MongoDB Javaドライバを使用して集計を実行することはできますが、「文字列形式」では実行することはできません。Javaドライバの集計ヘルパーを使用して集計コマンドのJava形式を作成します。この詳細についての詳細はin the docsです。ここで
は... 3.xのMongoDBのJavaのドライバを使用して(未テスト)の例です
MongoCollection<Document> collection = mongoClient.getDatabase("...").getCollection("...");
AggregateIterable<Document> documents = collection.aggregate(Arrays.asList(
// the unwind stage
new Document("$unwind", "$specs"),
// the lookup stage
new Document("$lookup", new Document("from", "inventory")
.append("localField", "specs")
.append("foreignField", "size")
.append("as", "inventory_docs")),
// the match stage
new Document("$match", new Document("inventory_docs", new BasicDBObject("$ne", new String[0])))
));
。これは、シェルスクリプトからJavaへの変換の形を見るために役立つかもしれません。
:http://mongodb.github.io/mongo-java-driver/3.5/driver/tutorials/aggregation/ – Marco
あなたはJavaドライバを使用して集約演算上記のことを表現する方法を求めていますか?または、上記の文字列をJavaドライバに渡してドライバで実行できるようにすることができますか? – glytching