2016-12-13 9 views
0

mongoTemplate集約クラスで$ setIsSubsetを使用しようとしています。しかし、解決策を見つけることができませんでした。

私モンゴクエリは次のとおりです。

db.events.aggregate([ 
    { $match: { $and: [ 
     { "event_state" : {$in : ["live","scheduled"]} }, 
     { "schedule.end_time" : {$gt : ISODate("2016-12-12T06:30:00.000Z")} } 
     ] 
     } 
    }, 
    {$project: 
     { 
      "name" :1, 
      "category" :1, 
      "schedule":1, 
      "celebrity" :1, 
      "online_moderator" :1, 
      "offline_moderator" :1, 
      "region" :1, 
      "status" :1, 
      "event_state" :1, 
      "recorder_id" :1, 
      "webcast_url":1, 
      "replay_url":1, 
      "registered_users":1, 
      registeredUsers: { $size:"$registered_users" }, 
      is_registered: 
      { $setIsSubset: [[ObjectId("584e6253e17ed10f0a8cba1d"),ObjectId("583e9719e17e8c1bf80da2fe")], "$registered_users.user_id"]} 
     } 
    }, 
    { $sort : { 
      "schedule.start_time": 1 
     } 
    } 
]); 

私は以下のようなコードのほとんどを変換しています。しかし、$ setIsSubset条件で打ちました。

matchCondition = Aggregation.match(Criteria.where("event_state") 
        .in(listEventStates).and("schedule.end_time").gt(d)); 

AggregationOperation projectValues = Aggregation.project() 
       .and("registered_users").size().as("registered_users_count") 
       .and("name").as("name").and("category").as("category") 
       .and("schedule").as("schedule").and("online_moderator") 
       .as("online_moderator").and("offline_moderator") 
       .as("offline_moderator").and("region").as("region") 
       .and("status").as("status").and("event_state") 
       .as("event_state").and("recorder_id").as("recorder_id") 
       .and("webcast_url").as("webcast_url").and("replay_url") 
       .as("replay_url"); 

sortCondition = Aggregation.sort(Sort.Direction.ASC, 
        "schedule.start_time"); 

Aggregation aggrigation = Aggregation.newAggregation(matchCondition, 
       sortCondition, projectValues); 

答えて

1

AggressionExpressionを使用してください。プロジェクトに以下の投影を含める。 1.8.5 spring mongoデータバージョンを使用してください。

and(new AggregationExpression() { 
    @Override 
    public DBObject toDbObject(AggregationOperationContext context) { 
     return new BasicDBObject("$setIsSubset", 
       Arrays.<Object> asList(
         Arrays.<Object> asList("584e6253e17ed10f0a8cba1d", 
           "583e9719e17e8c1bf80da2fe"), 
         "$registered_users.user_id")); 
    } 
}).as("is_registered")); 

あなたは、私はあなたのソリューションに取り組んでいたJava 8

and(context -> new BasicDBObject("$setIsSubset", 
     Arrays.<Object> asList(
       Arrays.<Object> asList("584e6253e17ed10f0a8cba1d", 
         "583e9719e17e8c1bf80da2fe"), 
       "$registered_users.user_id"))) 
.as("is_registered")); 
+0

ありがとう@Sagar Reddy。あなたのソリューションはうまくいきました(ラムダ式ではありません)。しかし、小さな修正が必要です。つまり、setIsSubset演算子 "$ setIsSubset"ではなく、 "$$ setIsSubset"ではなく、$ setIsSubset演算子には$が1つだけ存在する必要があります。また、依存性に関する注釈(1.8.5)もありがとう。鉱山は1.8.0だった。だから私はそれに応じて変更しました。 – Kiran

0

ます。また試合(com.mongodb.client.model.Aggregates.match(BSONフィルタ))のためのプロジェクト(com.mongodb.client.model.Aggregates.project)のためのMongoDB APIで作業することができます

List <Bson> aggregationInput = new ArrayList <Bson>(); 
aggregationInput.add(match(eq("_id",id))); 
HashMap<String, Integer> fieldsMap=new HashMap<String, Integer>(); 
fieldsMap.put("sensorId", 1); 
fieldsMap.put("sensorModel", 1); 

ArrayList< ArrayList<String>> isSubset = new ArrayList< ArrayList<String>>(); 
ArrayList<String> isSubsetParameter1 = new ArrayList<String>(); 
isSubsetParameter1.add("$$attachment.mimeType"); 

ArrayList<String> isSubsetParameter2 = new ArrayList<String>(); 
isSubset.add(isSubsetParameter1); 
isSubset.add(isSubsetParameter2); 

Document docFields = new Document(); 
if (mimeTypes == null || mimeTypes.isEmpty()) { 
:私はちょうど

db.getCollection('collection').aggregate(
{ $match : {"_id" : "dff26f9c-350b-4bd5-bc62-62c19f21100c"} }, 
{ "$project": { 
     "sensorId": 1, "sensorModel": 1, 
    "attachments": { 
    "$filter": { 
    "input": "$attachments", 
    "as": "attachment", 
    "cond": { 
    "$setIsSubset": [ 
     ["$$attachment.type"], 
     ["StructAttachment", "BlobAttachment"] 
    ] 
    } } } }}) 

とJavaのコードは次のようである。このクエリで$ setIsSubset

を使用するための例のように仕上げたコードを使用することができます

// mimeTypesは、セット です。isSubsetParameter2.addAll(mimeTypes);

Document filter = new Document(); 
filter.put("input", "$attachments"); 
filter.put("as", "attachment"); 
filter.put("cond", new Document("$setIsSubset", isSubset)); 

docFields.put("attachments", new Document("$filter",filter)); 
docFields.putAll(fieldsMap); 
aggregationInput.add(project(docFields)); 

AggregateIterable<Document> resultIteratr = mongo.getDatabase("db").getCollection("collection") 
.aggregate(aggregationInput); 

私はこれがうまくいきたいと思います!

+0

のためのラムダとして式をラップすることができます。しかし、私はisSubsetにオブジェクトを追加しようとしています。パラメータの1つは文字列であり、もう1つはオブジェクトです。例:isSubsetParameter1.add( "$$ registered_users.user_id"); 2番目の値はuser_idで、これはObjectId型です。 – Kiran

+0

残念なことに、解決策(@ Guel135)が私のために働いていないことを示唆しています。文字列とオブジェクトの両方を同じ配列リストデータ "isSubset"に使用する必要があるためです。だから私はサブ文書をルーピングすることによって、これを達成する他の方法をいくつか使用しました。 – Kiran

関連する問題