2017-11-13 3 views
0

ニュースグループスタイルのアプリの投稿を並べ替えようとしているので、固定された投稿が最初に表示され、最新の日付順でソートされ、その後は他のすべての投稿が最新の日付順にソートされます。 Date.now()は真夜中の2つの日付の間にありますが、指定された時間だけ固定されます。ニュースグループスタイルの投稿を日付でソートし、上部に「固定」または「スティッキー」の投稿を並べ替えます。 MongoDB

各文書には、次のようになります。

{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b480e"), 
    "date" : ISODate("2017-11-02T00:00:00.000+0000"), 
    "type" : "published", 
    "detail" : { 
     "title" : "First! - Sticky today", 
     "author" : "Me", 
     "content" : "This post IS sticky on today: 11/12 < 11/13 < 11/18" 
    "isSticky" : { 
     "begin" : ISODate("2017-11-12T00:00:00.000+0000"), 
     "end" : ISODate("2017-11-18T00:00:00.000+0000") 
    } 
} 
{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b480f"), 
    "date" : ISODate("2017-11-01T00:00:00.000+0000"), 
    "type" : "published", 
    "detail" : { 
     "title" : "Second - Sticky today", 
     "author" : "Me", 
     "content" : "This post IS sticky on today: 11/12 < 11/13 < 11/22" 
    "isSticky" : { 
     "begin" : ISODate("2017-11-12T00:00:00.000+0000"), 
     "end" : ISODate("2017-11-22T00:00:00.000+0000") 
    } 
} 
{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b480h"), 
    "date" : ISODate("2017-11-10T00:00:00.000+0000"), 
    "type" : "published", 
    "detail" : { 
     "title" : "Third", 
     "author" : "Me", 
     "content" : "Newest NON-sticky post. Never was sticky." 
    "isSticky" : { 
     ""), 
     "") 
    } 
} 
{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b4811"), 
    "date" : ISODate("2017-11-09T00:00:00.000+0000"), 
    "type" : "published", 
    "detail" : { 
     "title" : "Fourth", 
     "author" : "You", 
     "content" : "No longer sticky: 11/09 < 11/13 !< 11/11" 
    "isSticky" : { 
     "begin" : ISODate("2017-11-09T00:00:00.000+0000"), 
     "end" : ISODate("2017-11-11T00:00:00.000+0000") 
    } 
} 
{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b4812"), 
    "date" : ISODate("2017-11-08T00:00:00.000+0000"), 
    "type" : "published", 
    "detail" : { 
     "title" : "Last", 
     "author" : "Me", 
     "content" : "Never was a sticky post" 
    "isSticky" : { 
     ""), 
     "") 
    } 
} 

...明確にするために、これらの文書は、すでに同じ順序である私は、彼らが一度にソート表示させたいと思います(ので、私の仕事が行われています!)。私は、その日付で、ちょうど最初isSticky.beginでソート

{ "isSticky.begin":-1, date: -1 } 

を試してみましたが、それらはスティッキー日付外にあるいったんそれがスティッキーポストが「有効期限切れ」と、リスト内のその場所に戻ることができるように動作しません。範囲、例えば上記の "Fourth"というタイトルの投稿。投稿をいつでも随時スティッキーにすることができるか、または将来スティッキーになるように設定することができるという要件もあります。スティッキーな日付範囲が作成日よりも優先され、作成日はスティッキーな日付範囲の始まり。

私はまだmongo初心者ですので、私はまだgrokkedしていないドキュメントのいくつかの概念があると確信しています。

答えて

0

あなたはこの方法でaggregate pipeline

を使用することができます。

> db.posts.aggregate([{$project: {document: "$$ROOT", "s": {$gte: ["$detail.isSticky.end", new Date() ] } } }, {$sort: {"s": -1, "date": -1}} , {$replaceRoot: {newRoot: '$document'}}]).pretty() 
{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b480e"), 
    "date" : ISODate("2017-11-02T00:00:00Z"), 
    "type" : "published", 
    "detail" : { 
     "title" : "First! - Sticky today", 
     "author" : "Me", 
     "content" : "This post IS sticky on today: 11/12 < 11/13 < 11/18", 
     "isSticky" : { 
      "begin" : ISODate("2017-11-12T00:00:00Z"), 
      "end" : ISODate("2017-11-18T00:00:00Z") 
     } 
    }, 
    "s" : true 
} 
{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b480f"), 
    "date" : ISODate("2017-11-01T00:00:00Z"), 
    "type" : "published", 
    "detail" : { 
     "title" : "Second - Sticky today", 
     "author" : "Me", 
     "content" : "This post IS sticky on today: 11/12 < 11/13 < 11/22", 
     "isSticky" : { 
      "begin" : ISODate("2017-11-12T00:00:00Z"), 
      "end" : ISODate("2017-11-22T00:00:00Z") 
     } 
    }, 
    "s" : true 
} 
{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b480a"), 
    "date" : ISODate("2017-11-10T00:00:00Z"), 
    "type" : "published", 
    "detail" : { 
     "title" : "Third", 
     "author" : "Me", 
     "content" : "Newest NON-sticky post. Never was sticky.", 
     "isSticky" : { 

     } 
    }, 
    "s" : false 
} 
{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b4811"), 
    "date" : ISODate("2017-11-09T00:00:00Z"), 
    "type" : "published", 
    "detail" : { 
     "title" : "Fourth", 
     "author" : "You", 
     "content" : "No longer sticky: 11/09 < 11/13 !< 11/11", 
     "isSticky" : { 
      "begin" : ISODate("2017-11-09T00:00:00Z"), 
      "end" : ISODate("2017-11-11T00:00:00Z") 
     } 
    }, 
    "s" : false 
} 
{ 
    "_id" : ObjectId("59b6dab93294aebe7d8b4812"), 
    "date" : ISODate("2017-11-08T00:00:00Z"), 
    "type" : "published", 
    "detail" : { 
     "title" : "Last", 
     "author" : "Me", 
     "content" : "Never was a sticky post", 
     "isSticky" : { 

     } 
    }, 
    "s" : false 
} 

$projectステージはポストがstickyand「文書」フィールドに文書を保存しているかどうかを示す「S」のフィールドを追加します。

その後$sortソート

そして$replaceRootこれが適切にソートされたデータを見るために動作しますが、それは全体が変更されるため、私のangular2 +フロントエンドを破る

+0

$プロジェクト段階から文書でルートを交換を行います結果(元のドキュメントをドキュメントフィールドに埋め込むことによって)、そうですね、私はそのステップなしで同じ結果を達成したいと思います。 $プロジェクトを使用して "s"フィールドだけを追加できますか?スティッキーポストに小さなピンアイコンを表示するためにそのフィールドを使用できるかもしれませんが、私はこのコレクションへのすべての参照を "ドキュメント"を含むように変更する必要はないと思います。接頭辞。 –

+0

これで完了です。私はその後、$ replaceRootステージを追加してドキュメントをリセットします –

関連する問題