【发布时间】:2016-08-01 16:17:30
【问题描述】:
我在 mongodb 的 users 集合下有一个类别数组。
前端基本上有一个类别的拖放菜单,当发生拖放时我想更新数据库。由于无法在数组中移动项目,我决定在 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 : 用户对象 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(旧索引)
- B 类 - 1
- C 类 - 2
- D 类 - 3
将 A 移动到索引 2
- B 类 - 0 (-1)
- C 类 - 1 (-1)
- A 类 - 2(新索引)
- D 类 - 3 (0)
反之亦然
if (oldIndex > newIndex)
- A 类 - 0
- B 类 - 1
- C 类 - 2(旧索引)
- D 类 - 3
将 C 移动到索引 0
- C 类 - 0(新索引)
- A 类 - 1 (+1)
- B 类 - 2 (+1)
- D 类 - 3 (0)
在这种情况下,我无法运行查找查询来查看查询结果并对其进行故障排除。
对于任意数量的类别,查询只会更新一个值。
我想我需要一双新的眼睛。
谢谢。
【问题讨论】: