0

mongodb 3.2の Java ドライバーを使用して $lookup コレクションで集計 $match を実行する方法を知りたいです。私が取り組んでいる2つのコレクションの構造は次のとおりです。

coll_one:{
_id : ObjectId("hex_string"),
foreign_id : ObjectId("hex_string") **the id of coll_two**}

coll_two:{
_id : ObjectId("hex_string"),
actif : true,  
closed : false }

2 つの ID (coll_one.foreign_id & coll_two._id) のルックアップは正常に機能しているようです。しかし、coll_two.actif = true で一致を指定すると、空の結果が返されます。

これは私が使用しているJavaコードです:

Bson lookup = new Document("$lookup", 
new Document("from", "coll_two"  )
.append("localField", "foreign_id")
.append("foreignField", "_id")
.append("as", "look_coll"));

List<Bson> filters = new ArrayList<Bson>();
filters.add(lookup);

//here is the MATCH
filters.add(match(eq("look_coll.actif",true))); 

DB().getCollection("coll_one").aggregate(filters);

一致セクションを削除すると、すべてが正常に機能します。私は非常に多くの可能性の組み合わせを試しましたが、まったく成功しませんでした!!!

これが可能かどうか教えてもらえますか????

4

1 に答える 1

0

$matchリクエストをDocumentインスタンスに追加します:

Bson match = new Document("$match",
                new Document("look_coll.actif", true));

filters.add(match);

完全な例を次に示します。

MongoClient mongoClient = new MongoClient("localhost");

MongoDatabase db = mongoClient.getDatabase("mydb");

Bson lookup = new Document("$lookup",
        new Document("from", "coll_two")
                .append("localField", "foreign_id")
                .append("foreignField", "_id")
                .append("as", "look_coll"));

Bson match = new Document("$match",
        new Document("look_coll.actif", true));

List<Bson> filters = new ArrayList<>();
filters.add(lookup);
filters.add(match);

AggregateIterable<Document> it = db.getCollection("coll_one").aggregate(filters);

for (Document row : it) {
    System.out.println(row.toJson());
}
于 2016-07-30T14:18:21.243 に答える