Phi*_*thé 129 mongoose mongodb node.js
我找不到排序修饰符的文档.唯一的见解是在单元测试中: spec.lib.query.js#L12
writer.limit(5).sort(['test', 1]).group('name')
Run Code Online (Sandbox Code Playgroud)
但它对我不起作用:
Post.find().sort(['updatedAt', 1]);
Run Code Online (Sandbox Code Playgroud)
Der*_*ner 128
这就是我在mongoose 2.3.0中工作的方式:)
// Find First 10 News Items
News.find({
deal_id:deal._id // Search Filters
},
['type','date_added'], // Columns to Return
{
skip:0, // Starting Row
limit:10, // Ending Row
sort:{
date_added: -1 //Sort by Date Added DESC
}
},
function(err,allNews){
socket.emit('news-load', allNews); // Do something with the array of 10 objects
})
Run Code Online (Sandbox Code Playgroud)
iwe*_*ein 128
在Mongoose中,可以通过以下任何方式进行排序:
Post.find({}).sort('test').exec(function(err, docs) { ... });
Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
Post.find({}).sort({test: 1}).exec(function(err, docs) { ... });
Post.find({}, null, {sort: {date: 1}}, function(err, docs) { ... });
Run Code Online (Sandbox Code Playgroud)
小智 54
截至Mongoose 3.8.x:
model.find({ ... }).sort({ field : criteria}).exec(function(err, model){ ... });
Run Code Online (Sandbox Code Playgroud)
哪里:
criteria可以是asc,desc,ascending,descending,1,或-1
小智 51
尝试:
Post.find().sort([['updatedAt', 'descending']]).all(function (posts) {
// do something with the array of posts
});
Run Code Online (Sandbox Code Playgroud)
AJ.*_*AJ. 23
更新
如果这让人感到困惑,那就更好了.检查查找文档以及查询如何在mongoose手册中工作.如果要使用流畅的api,可以通过不向find()方法提供回调来获取查询对象,否则可以按照下面的概述指定参数.
原版的
给定一个model对象,根据Model上的文档,这是它如何工作2.4.1:
Post.find({search-spec}, [return field array], {options}, callback)
Run Code Online (Sandbox Code Playgroud)
该search spec预期的目标,但你可以通过null或空对象.
第二个参数是字段列表作为字符串数组,因此您将提供['field','field2']或null.
第三个参数是作为对象的选项,其中包括对结果集进行排序的功能.你可以使用{ sort: { field: direction } }其中field的字符串字段名test(你的情况),并direction是一个数,其中1被上升和-1被desceding.
最后一个param(callback)是回调函数,它接收查询返回的文档集合.
在Model.find()实现(在这个版本)确实性质来处理可选则params的滑动分配(这是困惑我!):
Model.find = function find (conditions, fields, options, callback) {
if ('function' == typeof conditions) {
callback = conditions;
conditions = {};
fields = null;
options = null;
} else if ('function' == typeof fields) {
callback = fields;
fields = null;
options = null;
} else if ('function' == typeof options) {
callback = options;
options = null;
}
var query = new Query(conditions, options).select(fields).bind(this, 'find');
if ('undefined' === typeof callback)
return query;
this._applyNamedScope(query);
return query.find(callback);
};
Run Code Online (Sandbox Code Playgroud)
HTH
小智 18
您可以按以下方式对查询结果进行排序
Post.find().sort({createdAt: "descending"});Run Code Online (Sandbox Code Playgroud)
sul*_*lam 15
猫鼬v5.4.3
按升序排序
Post.find({}).sort('field').exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'asc' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'ascending' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: 1 }).exec(function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'asc' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'ascending' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : 1 }}), function(err, docs) { ... });
Run Code Online (Sandbox Code Playgroud)
按降序排序
Post.find({}).sort('-field').exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'desc' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'descending' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: -1 }).exec(function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'desc' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'descending' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : -1 }}), function(err, docs) { ... });
Run Code Online (Sandbox Code Playgroud)
有关详细信息:https : //mongoosejs.com/docs/api.html#query_Query-sort
Sal*_*are 11
这就是我在mongoose.js 2.0.4中工作的方式
var query = EmailModel.find({domain:"gmail.com"});
query.sort('priority', 1);
query.exec(function(error, docs){
//...
});
Run Code Online (Sandbox Code Playgroud)
小智 10
使用Mongoose 4中的查询构建器界面进行链接.
// Build up a query using chaining syntax. Since no callback is passed this will create an instance of Query.
var query = Person.
find({ occupation: /host/ }).
where('name.last').equals('Ghost'). // find each Person with a last name matching 'Ghost'
where('age').gt(17).lt(66).
where('likes').in(['vaporizing', 'talking']).
limit(10).
sort('-occupation'). // sort by occupation in decreasing order
select('name occupation'); // selecting the `name` and `occupation` fields
// Excute the query at a later time.
query.exec(function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host
})
Run Code Online (Sandbox Code Playgroud)
有关查询的更多信息,请参阅文档.
小智 6
app.get('/getting',function(req,res){
Blog.find({}).limit(4).skip(2).sort({age:-1}).then((resu)=>{
res.send(resu);
console.log(resu)
// console.log(result)
})
})
Run Code Online (Sandbox Code Playgroud)
输出
[ { _id: 5c2eec3b8d6e5c20ed2f040e, name: 'e', age: 5, __v: 0 },
{ _id: 5c2eec0c8d6e5c20ed2f040d, name: 'd', age: 4, __v: 0 },
{ _id: 5c2eec048d6e5c20ed2f040c, name: 'c', age: 3, __v: 0 },
{ _id: 5c2eebf48d6e5c20ed2f040b, name: 'b', age: 2, __v: 0 } ]
Run Code Online (Sandbox Code Playgroud)
使用当前版本的 mongoose (1.6.0) 如果您只想按一列排序,则必须删除数组并将对象直接传递给 sort() 函数:
Content.find().sort('created', 'descending').execFind( ... );
Run Code Online (Sandbox Code Playgroud)
我花了一些时间来解决这个问题:(
| 归档时间: |
|
| 查看次数: |
224515 次 |
| 最近记录: |