Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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 如何使用node.js和mongoose在mongodb中按排序打印所有值_Javascript_Node.js_Mongodb_Sorting - Fatal编程技术网

Javascript 如何使用node.js和mongoose在mongodb中按排序打印所有值

Javascript 如何使用node.js和mongoose在mongodb中按排序打印所有值,javascript,node.js,mongodb,sorting,Javascript,Node.js,Mongodb,Sorting,我正在尝试使用node.js和mongodb 下面是来自 当我运行这段代码时,我得到以下输出: { _id: 566526a6d811e5a81f4bfa55, a: 1 } //ascending { _id: 566526a6d811e5a81f4bfa57, a: 3 } //descending 但通常,当我们执行find()和sort()时,我们会以升序或降序的顺序得到表中所有值的列表 但是在这里,我只得到排序操作中的第一个值 我希望输出如下: { _id: 566526a6d81

我正在尝试使用node.js和mongodb

下面是来自

当我运行这段代码时,我得到以下输出:

{ _id: 566526a6d811e5a81f4bfa55, a: 1 } //ascending

{ _id: 566526a6d811e5a81f4bfa57, a: 3 } //descending
但通常,当我们执行
find()
sort()
时,我们会以
升序
降序
的顺序得到表中所有值的列表

但是在这里,我只得到排序操作中的第一个值

我希望输出如下:

{ _id: 566526a6d811e5a81f4bfa55, a: 1 } //ascending
{ _id: 566526a6d811e5a81f4bfa55, a: 2 }
{ _id: 566526a6d811e5a81f4bfa57, a: 3 }


{ _id: 566526a6d811e5a81f4bfa55, a: 3 } //descending
{ _id: 566526a6d811e5a81f4bfa55, a: 2 }
{ _id: 566526a6d811e5a81f4bfa55, a: 1 }
有人能解释一下这里的错误吗?

这是因为,它只得到下一个文档。要显示所有文档,请使用:

这是因为,它只获取下一个文档。要显示所有文档,请使用:

因为
sort()
返回一个游标对象。要对其进行迭代,可以使用
toArray()
each()

它与
toArray()
非常相似,不同之处在于
toArray()
完全迭代光标并在回调函数的第二个参数中加载所有文档

each()
方法一样,游标将只保存最多批大小的元素,并反复迭代,直到游标用尽为止

出于演示目的,您可以使用任何。但在现实世界的应用程序中,我认为
each()
的性能更好

,因为
sort()
返回一个游标对象。要对其进行迭代,可以使用
toArray()
each()

它与
toArray()
非常相似,不同之处在于
toArray()
完全迭代光标并在回调函数的第二个参数中加载所有文档

each()
方法一样,游标将只保存最多批大小的元素,并反复迭代,直到游标用尽为止


出于演示目的,您可以使用任何。但在现实世界的应用程序中,我认为
each()
的性能更好

它在控制台中打印
undefined
。因为存在错误,排序语法是针对游标的。我正在编辑我的答案。错误日志:
{[MongoError:n/a]name:'MongoError',message:'n/a','$err':'cannotcanonicalizequery:BadValue bad order array[2]',code:17287}
排序参数已更改,您仍在使用数组语法。因为每次运行代码时,它都会再次创建3行。看看这个_id,它们都是不同的对象。如果每次都要清理集合,只需放置
collection.remove()。它正在控制台中打印
未定义的
。因为存在错误并且排序语法用于游标。我正在编辑我的答案。错误日志:
{[MongoError:n/a]name:'MongoError',message:'n/a','$err':'cannotcanonicalizequery:BadValue bad order array[2]',code:17287}
排序参数已更改,您仍在使用数组语法。因为每次运行代码时,它都会再次创建3行。看看这个_id,它们都是不同的对象。如果每次都要清理集合,只需放置
collection.remove()在集合声明之后。
{ _id: 566526a6d811e5a81f4bfa55, a: 1 } //ascending
{ _id: 566526a6d811e5a81f4bfa55, a: 2 }
{ _id: 566526a6d811e5a81f4bfa57, a: 3 }


{ _id: 566526a6d811e5a81f4bfa55, a: 3 } //descending
{ _id: 566526a6d811e5a81f4bfa55, a: 2 }
{ _id: 566526a6d811e5a81f4bfa55, a: 1 }
// Do normal ascending sort
collection.find().sort({ a: 1 }).toArray(function (err, items) {
    if (err) console.log(err);
    console.log(items);

    // Do normal descending sort
    collection.find().sort({ a: -1 }).toArray(function (err2, items2) {
        if (err2) console.log(err2);
        console.log(items2);
        db.close();
    });
});
var MongoClient = require('mongodb').MongoClient,
    test = require('assert');

    MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
        // Create a collection
        var collection = db.collection('simple_sort_collection');

        // Insert some documents we can sort on
        collection.insertMany([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) {
            test.equal(null, err);

            // Do normal ascending sort
            collection.find().sort({'a': 1}).each(function(err, item) {
                test.equal(null, err);

                if (item != null) {
                    console.log(item);
                } else {
                    console.log('====================')
                    collection.find().sort({a: -1}).each(function(err, item) {
                        test.equal(null, err);

                        if (item != null) {
                            console.log(item);
                        } else {
                            db.close();
                        }
                    });
                }
           });
       });
  });