-3

grails(2.3.7) mongodb gormプラグインmongodb:3.0.1を使用しています。 私はdbに次のコレクションを持っています

 {
        "_id" : ObjectId("567eac392c56fd49950e2441"),
        "comments" : [
                {
                        "commentText" : "test comments!",
                        "userId" : "patient@gmail.com",
                        "likes" : 10,
                        "date" : "2015-12-25T10:34:53.048Z"
                },
                {
                        "commentText" : "master piece",
                        "userId" : "patient@gmail.com",
                        "likes" : 12,
                        "date" : "2015-12-25T10:34:53.052Z"
                },
                {
                        "commentText" : "test comments!",
                        "userId" : "patient@gmail.com",
                        "likes" : 10,
                        "date" : "2015-12-25T10:34:53.048Z"
                },
                {
                        "commentText" : "master piece",
                        "userId" : "patient@gmail.com",
                        "likes" : 12,
                        "date" : "2015-12-25T10:34:53.052Z"
                }
        ],
        "doctorUserId" : "doctor2@gmail.com",
        "recommendation" : 0,
        "version" : NumberLong(2)
}

ここで、mongoDB gorm を使用して、日付順 (コメント内) でコメント パラメータ内のクエリを実行したいと考えています。

前もって感謝します

4

2 に答える 2

0

Volodymyr Synytskyi の回答を Grails/GORM に変換する:

DBObject unwind = new BasicDBObject(['$unwind': '$comments']);

DBObject sort = new BasicDBObject(['$sort': [
    'comments.date' : 1
]]);

DBObject group = new BasicDBObject(['$group': [
    '_id'           : '$_id', 
    'comments'      : ['$push' : '$comments'], 
    'version'       : ['$first': '$version'], 
    'doctorUserId'  : ['$first': '$doctorUserId'],
    'recommendation': ['$first': '$recommendation']
]]);

DBColleciton collection = DoctorSocial.collection;
AggregationOutput aggregationOutput = collection.aggregate([unwind, sort, group]);
aggregationOutput.results().each { dbObject ->
    // Do something with your results
}

注:上記はテストされていないため、調整が必要な場合がありますが、同様の集計の使用は私のアプリで問題なく機能します。

于 2016-02-26T17:45:52.543 に答える