Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this kind of collection :

"File" 
{ 
"_id" : { "$oid" : "4f730e3bb8be296910000180"}
, "Name" : "File1.jpg"
, "Folders" : [ 
                { "F_id" : { "$oid" : "4f72f503b8be296d78000166"} , "Ord" : 1} 
                , { "F_id" : { "$oid" : "4f730eedb8be296e78000180"} , "Ord" : 3}
            ]
}
{ 
"_id" : { "$oid" : "4f730e3ab8be296978000181"} 
, "Name" : "File2.jpg"
, "Folders" : [ 
                { "F_id" : { "$oid" : "4f72f503b8be296d78000166"} , "Ord" : 2} 
                , { "F_id" : { "$oid" : "4f730eedb8be296e78000180"} , "Ord" : 2}
                , { "F_id" : { "$oid" : "4f730eedb8be296e78000132"} , "Ord" : 1}
            ] 
}
{ 
"_id" : { "$oid" : "4f730e38b8be296e78000182"}
, "Name" : "File3.jpg"
, "Folders" : [ 
                { "F_id" : { "$oid" : "4f72f503b8be296d78000166"} , "Ord" : 3} 
                , { "F_id" : { "$oid" : "4f730eedb8be296e78000180"} , "Ord" : 1}
            ]
}

"Folders" can contains a lot of more elements, and reflects in which Folder "F_id" the file is available, and at what order it take place "Ord" in this folder.

Then I would like to simply get all the Files available in one Folder in the correct Order.

But when I do something like :

File.find( {"Folders.F_id":{"$oid":4f72f503b8be296d78000166} } ).sort({"Folders.$.Ord":1}) ;

I can't get the correct Order I expected !

Any idea ?? Should I use Map reduce for that ? How ??

Thanks !

share|improve this question
add comment

2 Answers 2

Sorting in mongodb with .sort() only refers to sorting the documents themselves, not the values inside an array that are nested within those documents. With this schema, you will need to fetch the document and sort the values inside the array on the client side.

share|improve this answer
add comment

Thank you.

I found a work around with map/reduce, that works well for this case.

I put it here in case someone else is interrested :

//
//$folder_Id is the _id of the folder I want to list 
//map function
$mapFunc="function() {
                    var myFolder='". $folder_Id ."';
                    var obj = { // all the elements I need!
                        'Folders': {},
                        'Flds': {},
                        'Crea': {},
                        'TechInfo': {},
                        'Thumbs': {},
                        '_id': null,
                        'Order': null
                    }
                    if (this.Folders)obj.Folders = this.Folders ;
                    if (this.Flds)obj.Flds = this.Flds ;
                    if (this.Crea)obj.Crea = this.Crea ;
                    if (this.TechInfo)obj.TechInfo = this.TechInfo ;
                    if (this.Thumbs)obj.Thumbs = this.Thumbs ;
                    obj._id = this._id ;
                    if (this.Folders) {
                        for(var i=0 ; i< this.Folders.length ;i++){
                            if(this.Folders[i].F_id==myFolder){
                                obj.Order=  this.Folders[i].Ord
                                break;
                            }
                        }
                    }
                    emit( this._id , obj);
                }
        ";

//reduce function
$reduceFunc="function(key, values) {
                    values['_id']=key;
                    return  values  ;
                }
            ";


$mapReduceColl="MR_" . new MongoId();
$command = array(
    'mapreduce' => "File"
    ,'map' => $mapFunc
    ,'reduce' => $reduceFunc
    ,'query' => $query
    //,"verbose"=>true
    ,"out"=>array("replace"=>$mapReduceColl)
);
$statsInfo =  $db->MyDB->command($command);
$statsCollection =  $db->MyDB->selectCollection($statsInfo['result']);
$cursor = $statsCollection->find()->sort(array( $params["ordBy"]=>$params["ordByDir"]))->skip( $skip )->limit(  $nbreParPage );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.