0

I have lets say 2 collections , profile and followers . Profiles contain some ..profile informations (characteristics , photo id, etc..) and followers contain the profile _ID reference of followed . I attached the sample of this 2 collection bellow . We see in followers collection the last followers of bob(_id 1) are 4,2,3 respectively prince,jhon , alice . The _id 3 is alice and this is the last inserted .

I need to display all profiles informations(contain in profile collection) of each last followers of bob in the logical order " new before older " . I guess it need 2 querys but even that i didn't find solution / feature to do that , is it possible ? If you know if not possible in 2 querys , i am totatly open for others patterns advises .

For the example given above , the result expected (note the order is 3 , 2 , 4)should be :

{
    "_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

}

the profile collection sample :

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   
}

followers collection sample :

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]
}
Community
  • 1
  • 1
jess
  • 337
  • 3
  • 13

1 Answers1

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

Since you need to preserve the order of arguments in followers You will need to use mapReduce as Neil Lunn suggested 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
}
Community
  • 1
  • 1
styvane
  • 59,869
  • 19
  • 150
  • 156