Node,MongoDB:使用范围查询

时间:2016-08-01 16:17:31

标签: node.js mongodb

我在mongodb的用户集合下有一系列类别。

基本上前端有一个类别的拖放菜单,当发生拖放时我想更新数据库。由于无法在数组中移动项目,我决定在mongodb中给它们一个位置值“pos”,然后在angularjs中对它们进行排序。

categories: [
    {
        "_id" : ObjectId("5776782a57c9580256536656"),
        "title" : "Cars",
        "url" : "cars",
        "pos" : 0
    },
    {
        "_id" : ObjectId("5776786afdcd6ed056edff25"),
        "title" : "Photography",
        "url" : "photography",
        "nodes" : [ 
            {
                "_id" : ObjectId("57767870fdcd6ed056edff26"),
                "title" : "Landscape",
                "url" : "landscape"
            }
        ],
        "pos" : 1
    },
    {
        "_id" : ObjectId("adf9845782578a376161d079"),
        "title" : "Travel",
        "url" : "travel",
        "pos" : 2
    }
]

我正在使用nodejs-express作为服务器,这是我移动类别的代码。

  • oldIndex:移动前类别的位置
  • newIndex:移动后类别的新位置
  • userId:users object Id

    if (newIndex>oldIndex){
        db.collection('users').updateOne({ "_id": userId, "categories.pos": { $gte: oldIndex, $lt: newIndex } }, { $inc : { "categories.$.pos" : -1 } }, function (err, res) {
            console.log(res);
            if (err) callback(0)
            else {
                db.collection('users').updateOne(filter, { $set :  cdata }, function (err, res) {
                    if (err) callback(0);
                    callback(1);
                });
            }
        });
    } else {
        db.collection('users').updateOne({ "_id": userId, "categories.pos": { $gt: newIndex, $lte: oldIndex  } }, { $inc : { "categories.$.pos" : 1 } }, function (err, res) {
            console.log(res);
            if (err) callback(0)
            else {
                db.collection('users').updateOne(filter, { $set :  cdata }, function (err, res) {
                    if (err) callback(0);
                    callback(1);
                });
            }
        });
    }

基本上,代码意味着这样做

if(newIndex> oldIndex)

  • A类 - 0(oldIndex)
  • B类 - 1
  • C类 - 2
  • D - 3类别

将A移至指数2

  • B类 - 0(-1)
  • C类 - 1(-1)
  • A类 - 2(newIndex)
  • D类 - 3(0)

反之亦然

if(oldIndex> newIndex)

  • A类 - 0
  • B类 - 1
  • C - 2类(oldIndex)
  • D - 3类别

将C移至指数0

  • C类 - 0(newIndex)
  • A类 - 1(+1)
  • B类 - 2(+1)
  • D类 - 3(0)

在这种情况下,我无法运行查询查询以查看&排查查询结果..

包含任意数量的类别,查询只会更新一个值。

我想我需要新的一双眼睛。

谢谢。

1 个答案:

答案 0 :(得分:0)

MongoDB保持数组的顺序。 那么如何删除数组中的元素并使用$position

重新插入具有正确位置的元素

查询删除元素:

db.getCollection('users').update({
        "_id" : idusers
    },
    {
        $pull: {
            categories: {
                field: foo //your field for grep the element
            }
        }
})

用于在其新索引处设置元素的查询:

db.getCollection('users').update(
    {
        "_id" : idusers
    },
    {
        $push: {
            categories: {
               // Set just one element in the $each, that enable you to use $position
                $each: [{
                    "title" : "Photography",
                    "url" : "photography",
                    "nodes" : [{
                            "_id" : ObjectId("57767870fdcd6ed056edff26"),
                            "title" : "Landscape",
                            "url" : "landscape"
                    }]
                }],
                $position: newIndex
            }
        }
    }
)

我做了一些手动测试,似乎有效