【问题标题】:How to order the position of mongodb documents based on the position of items on a list. (meteor)如何根据列表中项目的位置对 mongodb 文档的位置进行排序。 (流星)
【发布时间】:2015-07-09 21:17:39
【问题描述】:
    Template.orionMaterializePagesIndex.rendered = function(){
        $(".table").sortable({
           items: ".odd,.even",
           delay: 100,
           refreshPositions: true,
           revert: true,
           helper: "clone",
           scroll: true,
           scrollSensitivity: 50,
           scrollSpeed: 35,
           start: function(event, ui) {
            $(ui.helper).addClass("dragging");
           }, // end of start
           stop: function(event, ui) {
            $(ui.item).removeClass("dragging");
           }, // end of stop
           update: function(event, ui) {
            var index = 0;


            _.each($(".odd,.even"), function(item) {
              orion.pages.collection.update({_id: item.id}, {
                $set:{
                  pos: index++,
                }
              });
            });
          } 
        }).disableSelection();
    }

这也是我的 monogodb 集合文档到生成的字段的图像-> http://s12.postimg.org/wp0c82yjx/Screen_Shot_2015_06_28_at_11_34_33_AM.png

【问题讨论】:

    标签: javascript jquery mongodb meteor


    【解决方案1】:

    您可以为集合中的每个文档添加一个字段“listPosition”。您可以按 listPosition 对文档集合进行排序,并根据需要更新每个文档的 listPosition。

    Collection.find({}, {sort: {listPosition: 1}});
    

    以上代码将返回排序后的文档。

    【讨论】:

    • 所以我添加了一个 listPosition 字段。但是每次我插入一个新文档时,listPosition 的值总是 1。查看要点gist.github.com/ecuanaso/ec4090f3bb3740b8cda3
    • @ecuanaso 此函数将始终在插入时返回 1。如果您要插入到列表的开头,这没关系,但您必须更新集合中所有其他文档的 listPosition。
    • 所以我必须更新我所有其他文档的其他 listPositions 吗?好的,您能否提供一个如何执行此操作的示例。我有 javascript 的工作知识。
    猜你喜欢
    • 2012-03-21
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多