Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在mongoDB中插入排序操作后的序列号?_Javascript_Mongodb_Sorting_Sql Update - Fatal编程技术网

Javascript 如何在mongoDB中插入排序操作后的序列号?

Javascript 如何在mongoDB中插入排序操作后的序列号?,javascript,mongodb,sorting,sql-update,Javascript,Mongodb,Sorting,Sql Update,我尝试使用JS代码按num_atoms对集合进行排序: db.zinc_data.find().sort({"num_atoms":-1}).forEach(function(mydoc){ db2 = connect("database:port/zinc_Purtest_sort"); db2.zinc_data.insert(mydoc); }) mongos> db.zinc_data.find({}).pretty() { "_id" : ObjectId("

我尝试使用JS代码按num_atoms对集合进行排序:

db.zinc_data.find().sort({"num_atoms":-1}).forEach(function(mydoc){ 
    db2 = connect("database:port/zinc_Purtest_sort");
    db2.zinc_data.insert(mydoc);
})

mongos> db.zinc_data.find({}).pretty()
{ "_id" : ObjectId("586e3ac78426af7af1425655"), "num_atoms" : 76, "idxd" : 5 }
{ "_id" : ObjectId("586e3ac88426af7af14256b2"), "num_atoms" : 74, "idxd" : 89}
{ "_id" : ObjectId("586e3ac78426af7af1425666"), "num_atoms" : 66, "idxd" : 64}
{ "_id" : ObjectId("586e3ac78426af7af1425690"), "num_atoms" : 63, "idxd" : 34}
{ "_id" : ObjectId("586e3ac48426af7af1425651"), "num_atoms" : 62, "idxd" : 1 }
{ "_id" : ObjectId("586e3ac88426af7af1425697"), "num_atoms" : 61, "idxd" : 96}
{ "_id" : ObjectId("586e3ac88426af7af14256a9"), "num_atoms" : 61, "idxd" : 97}
{ "_id" : ObjectId("586e3ac78426af7af1425685"), "num_atoms" : 60, "idxd" : 90}
{ "_id" : ObjectId("586e3ac78426af7af142568a"), "num_atoms" : 60, "idxd" : 61}
{ "_id" : ObjectId("586e3ac88426af7af14256a2"), "num_atoms" : 59, "idxd" : 66}
{ "_id" : ObjectId("586e3ac78426af7af142566e"), "num_atoms" : 58, "idxd" : 93}
{ "_id" : ObjectId("586e3ac78426af7af1425677"), "num_atoms" : 58, "idxd" : 62}
{ "_id" : ObjectId("586e3ac78426af7af1425683"), "num_atoms" : 58, "idxd" : 94}
{ "_id" : ObjectId("586e3ac78426af7af142568f"), "num_atoms" : 58, "idxd" : 30}
{ "_id" : ObjectId("586e3ac88426af7af14256aa"), "num_atoms" : 58, "idxd" : 63}
{ "_id" : ObjectId("586e3ac48426af7af1425652"), "num_atoms" : 57, "idxd" : 2 }
{ "_id" : ObjectId("586e3ac88426af7af14256ac"), "num_atoms" : 57, "idxd" : 85}
{ "_id" : ObjectId("586e3ac78426af7af1425676"), "num_atoms" : 56, "idxd" : 69}
{ "_id" : ObjectId("586e3ac78426af7af142567a"), "num_atoms" : 56, "idxd" : 58}
{ "_id" : ObjectId("586e3ac78426af7af142568e"), "num_atoms" : 56, "idxd" : 91}
然后我想用序列号更新idxd插入新序列号idxd2也可以,我尝试了:

var count=1;
db2.zinc_data.find().forEach(function(mydoc){ 
    db2.zinc_data.update({"idxd":{$gt:0}},{$set:{"idxd":count}});
    count++;
    print(count);
})
但它不起作用。这个集合有20000000行数据,所以我无法找到idxd并更新idxd,我只想在排序操作后获得序列号

这里有任何帮助,或者如果有人能给我指出一些好的文档或教程,无论哪种方式都会非常有帮助


谢谢

您可以在第一步中插入序列号

var counter = 0;
db.zinc_data.find().sort({"num_atoms":-1}).forEach(function(mydoc){ 
    db2 = connect("database:port/zinc_Purtest_sort");
    mydoc.idxd = ++counter;
    db2.zinc_data.insert(mydoc);
})
输出:

> db.<sorted_data_table>.find({}, {_id : 0})
{ "num_atoms" : 76, "idxd" : 1 }
{ "num_atoms" : 74, "idxd" : 2 }
{ "num_atoms" : 66, "idxd" : 3 }
{ "num_atoms" : 63, "idxd" : 4 }
{ "num_atoms" : 62, "idxd" : 5 }
{ "num_atoms" : 61, "idxd" : 6 }
{ "num_atoms" : 61, "idxd" : 7 }
{ "num_atoms" : 60, "idxd" : 8 }
{ "num_atoms" : 60, "idxd" : 9 }
{ "num_atoms" : 59, "idxd" : 10 }
{ "num_atoms" : 58, "idxd" : 11 }
{ "num_atoms" : 58, "idxd" : 12 }
{ "num_atoms" : 58, "idxd" : 13 }
{ "num_atoms" : 58, "idxd" : 14 }
{ "num_atoms" : 58, "idxd" : 15 }
{ "num_atoms" : 57, "idxd" : 16 }
{ "num_atoms" : 57, "idxd" : 17 }
{ "num_atoms" : 56, "idxd" : 18 }
{ "num_atoms" : 56, "idxd" : 19 }
{ "num_atoms" : 56, "idxd" : 20 }