2016-11-20 5 views
0

findで操作フィールドを除外できますが、findを実行する場合は、sort、その後はprojectionとします。あなたはどんなトリック、操作を知っていますか?ソート後にMongoDBでプロジェクトするには?

docfields {Object}, the fields to return in the query. Object of fields to include or exclude (not both), {‘a’:1}

答えて

2

あなたは条件、予測、およびソートして通常の検索クエリを実行することができます。私はあなたが投影したくないフィールドをソートしたいと思う。しかし、それを心配しないでください、あなたはそれを投影していなくても、そのフィールドでソートすることができます。

並べ替えフィールドの投影を「0」として明示的に選択すると、その検索クエリを実行できなくなります。それでも必要な結果を得ることはありません場合は

//This query will work 
    db.collection.find(
    {_id:'someId'}, 
    {'someField':1}) 
    .sort('someOtherField':1) 

    //This query won't work 
    db.collection.find(
    {_id:'someId'}, 
    {'someField':1,'someOtherField':0}) 
    .sort('someOtherField':1) 

しかし、MongoDB Aggregation Frameworkに見て!ここで

はあなたの条件に応じて集約のためのサンプルクエリをある

db.collection.aggregate([ 
{$match: {_id:'someId'}}, 
{$sort: {someField:1}}, 
{$project: {_id:1,someOtherField:1}}, 
]) 
関連する問題