2017-05-22 19 views
0

次のコードをSpringのデータ型Javaクエリに変換するにはどうすればよいですか?SpringデータMongoDB - ネストされたフィールド投影による集約

db.messages.aggregate([ 
    {$lookup:{from: "images", localField: "imageId", foreignField: "_id", as: "image"}}, 
    {$unwind: "$image"}, 
    {$project: {"text": 1, "liked": {$gt: [{$size: {$setIntersection: ['$image.likers', ['2']]}}, 0]}}} 
    ]) 

メッセージ:

{ 
    "_id": "1", 
    "text": "hi", 
    "imageId": "1" 
} 

画像:

{ 
    "_id": "1", 
    "likers": ["1","2","3"] 
} 
+0

基本的に私は/「気に入っ」と利用者Xがメッセージを好きなら、それは本当であるべきと呼ばれる新しいフィールドを追加しようとしています画像。 – shayy

+0

あなたのやりたいことがはっきりしません。詳細を説明してください – pvpkiran

答えて

1

あなたは1.10.3春モンゴバージョン/ 1.5.3春ブートバージョン3.4のmongoバージョンに集約下に試すことができます。

シェルクエリリファレンス

ため
db.messages.aggregate(
{ "$lookup" : { "from" : "images" , "localField" : "imageId" , "foreignField" : "_id" , "as" : "image"}} , 
{ "$unwind" : "$image"} , 
    { "$project" : { "text" : 1 , "liked" : { "$in" : [ "2" , "$image.likers"]}}} 
) 

春モンゴコード:

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; 
import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.arrayOf; 

Aggregation agg = newAggregation(
lookup("images", "imageId", "_id", "image"), 
unwind("image"), 
project("text").and(arrayOf("image.likers").containsValue("2")).as("liked") 
); 
+0

ここにarrayOfとは何ですか? – Sachin

+0

@Sachin 'arrayOf'は、配列演算子factoryへの参照を取得する静的ファクトリメソッドであるため、配列に対して操作を実行できます。このような操作の1つは、 '$ in'式'をレンダリングする 'containsValue'です。私が別の質問で言及したように、完全なリストは[here](https://docs.mongodb.com/manual/reference/operator/aggregation-array/) – Veeram

+0

をご覧ください。 containsValueのListを使用できますか? – Sachin

関連する問題