Javascript 如何使用node.js和mongoose在mongodb中按排序打印所有值
我正在尝试使用node.js和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
{ _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()集合声明后的code>。它正在控制台中打印未定义的
。因为存在错误并且排序语法用于游标。我正在编辑我的答案。错误日志:{[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();
}
});
}
});
});
});