2017-11-03 2 views
0

java-mongo-driverのみを使用してmongoネイティブクエリを実行する方法。mongo-java-driverのみを使用してMongoDBネイティブクエリ(JSON)を実行するにはどうすればよいですか?

ノー春データやEclipseLinkのまたは休止状態OGM、のみのjava-モンゴ・ドライバーを使用して

サンプルクエリ:

db.orders.aggregate([ 
    { 
     $unwind: "$specs" 
    }, 
    { 
     $lookup: 
     { 
      from: "inventory", 
      localField: "specs", 
      foreignField: "size", 
      as: "inventory_docs" 
     } 
    }, 
    { 
     $match: { "inventory_docs": { $ne: [] } } 
    } 
]) 
+0

:http://mongodb.github.io/mongo-java-driver/3.5/driver/tutorials/aggregation/ – Marco

+1

あなたはJavaドライバを使用して集約演算上記のことを表現する方法を求めていますか?または、上記の文字列をJavaドライバに渡してドライバで実行できるようにすることができますか? – glytching

答えて

0

ご質問がある場合:

上記の文字列を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への変換の形を見るために役立つかもしれません。

0

時間がありませんでしたので、次の回避策で使用しました。 Morcosの提案については後で詳しく説明します。 しかし、次のコードも動作します。公式ドキュメントから

import com.infrasoft.mongo.MongoClientFactory; 
import com.mongodb.BasicDBList; 
import com.mongodb.BasicDBObject; 
import com.mongodb.CommandResult; 
import com.mongodb.DB; 
import com.mongodb.DBObject; 
import com.mongodb.util.JSON; 

/** 
* 
* @author charudatta.joshi 
*/ 
public class TestNaiveQuery1 { 

    public static void main(String[] args) { 

     String nativeQuery = "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" 
       + "])"; 

     DBObject command = new BasicDBObject(); 
     DB db = MongoClientFactory.getMongoClientFactory().getMongoClient().getDB("frms_data_demo"); 

     nativeQuery = "function() { return (" + nativeQuery + ").toArray(); }"; 

     //command.put("eval", "function() { return db." + collectionName + ".find(); }"); 
     command.put("eval", nativeQuery); 
     CommandResult result = db.command(command); 

     BasicDBList dbObjList = (BasicDBList) result.toMap().get("retval"); 

     DBObject dbo0 = (BasicDBObject) dbObjList.get(0); 
     DBObject dbo1 = (BasicDBObject) dbObjList.get(0); 

     System.out.println(dbObjList.get(0)); 
     System.out.println(dbObjList.get(1)); 
     // .... just loop on dbObjList 

    } 


} 
関連する問題