简单的模式,由最后的粉丝排序

时间:2015-01-20 17:10:56

标签: mongodb mongodb-query aggregation-framework nosql

我可以说2个收藏,个人资料和关注者。配置文件包含一些..profile信息(特征,照片ID等),并且关注者包含所遵循的配置文件_ID参考。 我附上了 2 collection bellow 的样本。我们在追随者集合中看到bob(_id 1)的最后追随者分别是 4,2,3 王子,jhon,alice。 _id 3是alice,这是最后插入的。

我需要以逻辑顺序“new before older”显示bob的每个最后关注者的所有个人资料信息(包含在个人资料集合中)。 我想它需要2个查询,但即使我没有找到解决方案/功能来做到这一点,是否可能?如果您知道在2个查询中是否不可能,我会完全开放其他模式建议。

对于上面给出的示例,预期结果(请注意顺序为3,2,4)应为:

{
    "_id" : 3,
    "name" : "alice",
    "Region1" : "Alsace",
    "code_postal" : 67240,
    "ville" : "Oberhoffen-sur-Moder",
     "photo_id" : 7800098

}

{
    "_id" : 2,
    "name" : "jhon",
    "Region1" : "Alsace",
    "code_postal" : 67240,
    "ville" : "Oberhoffen-sur-Moder",
     "photo_id" : 7111987    
}

{
    "_id" : 4,
    "name" : "prince",
    "Region1" : "Alsace",
    "code_postal" : 67240,
    "ville" : "Oberhoffen-sur-Moder",
     "photo_id" : 6535098

}

个人资料收集示例:

db.profile.find()
{
    "_id" : 1,
    "name" : "bob",
    "Region1" : "Alsace",
    "code_postal" : 67240,
    "ville" : "Oberhoffen-sur-Moder",
    "photo_id" : 7863635

}

{
    "_id" : 2,
    "name" : "jhon",
    "Region1" : "Alsace",
    "code_postal" : 67240,
    "ville" : "Oberhoffen-sur-Moder",
     "photo_id" : 7111987    
}

{
    "_id" : 3,
    "name" : "alice",
    "Region1" : "Alsace",
    "code_postal" : 67240,
    "ville" : "Oberhoffen-sur-Moder",
     "photo_id" : 7800098   
}
{
    "_id" : 4,
    "name" : "prince",
    "Region1" : "Alsace",
    "code_postal" : 67240,
    "ville" : "Oberhoffen-sur-Moder",
     "photo_id" : 6535098   
}

关注者收集样本:

db.followers.find()

{
    "_id" : 1,
    "followers" : [4,2,3]
}


{
    "_id" : 2,
    "followers" : [8,2,3,11,39,653,]
}


{
    "_id" : 3,
    "followers" : [9,76,538,111,134]
}

1 个答案:

答案 0 :(得分:1)

var target = db.followers.find({_id: 1}).toArray();
var followers = target[0].followers.reverse();

由于您需要保留followers中参数的顺序,您需要使用mapReduce Neil Lunn 建议here

db.profile.mapReduce(
    function () {
        var order = inputs.indexOf(this._id);
        emit( order, { doc: this } );
    },
    function() {},
    { 
        "out": { "inline": 1 },
        "query": { "_id": { "$in": followers } },
        "scope": { "inputs": followers } ,
        "finalize": function (key, value) {
            return value.doc;
        }
    }
)


{
        "results" : [
                 {
                        "_id" : 0,
                        "value" : {
                                "_id" : 3,
                                "name" : "alice",
                                "Region1" : "Alsace",
                                "code_postal" : 67240,
                                "ville" : "Oberhoffen-sur-Moder",
                                "photo_id" : 7800098
                        }
                },
                {
                        "_id" : 1,
                        "value" : {
                                "_id" : 2,
                                "name" : "jhon",
                                "Region1" : "Alsace",
                                "code_postal" : 67240,
                                "ville" : "Oberhoffen-sur-Moder",
                                "photo_id" : 7111987
                        }
                },
                {
                        "_id" : 2,
                        "value" : {
                                "_id" : 4,
                                "name" : "prince",
                                "Region1" : "Alsace",
                                "code_postal" : 67240,
                                "ville" : "Oberhoffen-sur-Moder",
                                "photo_id" : 6535098
                        }
                }
        ],
        "timeMillis" : 1,
        "counts" : {
                "input" : 3,
                "emit" : 3,
                "reduce" : 0,
                "output" : 3
        },
        "ok" : 1
}