按日期对新闻组样式的帖子进行排序,并使用'固定'或者'粘性'在顶部的帖子。 MongoDB的

时间:2017-11-13 20:44:59

标签: mongodb mongodb-query

我试图为新闻组风格的应用排序帖子,因此固定的帖子首先显示,按日期排序最先排序,然后所有其他帖子显示按日期排序最新的第一个...添加了扭曲帖子在指定的时间内只是粘性的:而Date.now()在两个午夜日期之间。

每个文档都是这样的:

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

但是,这不会允许粘贴帖子过期&#39;一旦它们超出粘性日期范围,则返回列表中的位置,例如标题为'#34; Fourth&#34;以上。还要求帖子可以在任何时候追溯性地粘贴,或者可以设置为在将来变得粘稠...因此粘性日期范围优先于其创建日期,并且创建日期不必等于粘性日期范围的开始。

我还是一个mongo初学者,所以我确定在我还没有完成的文档中只有一些概念。

1 个答案:

答案 0 :(得分: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阶段添加一个“s”字段,指示帖子是否为粘性,并将文档存储在“文档”字段中。

然后$sort执行排序

$replaceRoot将$ root替换为$ project阶段的文档