태그 보관물: mongodb

mongodb

몽구스로 정렬하는 방법? 없습니다. 유일한 통찰력은

정렬 수정 자에 대한 문서가 없습니다. 유일한 통찰력은 단위 테스트에 있습니다 :
spec.lib.query.js # L12

writer.limit(5).sort(['test', 1]).group('name')

그러나 그것은 나를 위해 작동하지 않습니다 :

Post.find().sort(['updatedAt', 1]);



답변

몽구스에서는 다음과 같은 방법으로 정렬 할 수 있습니다.

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) { ... });


답변

이것은 몽구스 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
})


답변

몽구스 3.8.x 기준 :

model.find({ ... }).sort({ field : criteria}).exec(function(err, model){ ... });

어디:

criteria할 수있다 asc, desc, ascending, descending, 1, 또는-1


답변

최신 정보:

Post.find().sort({'updatedAt': -1}).all((posts) => {
  // do something with the array of posts
});

시험:

Post.find().sort([['updatedAt', 'descending']]).all((posts) => {
  // do something with the array of posts
});


답변

몽구스 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) { ... });

내림차순으로 정렬

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) { ... });

세부 사항 : https://mongoosejs.com/docs/api.html#query_Query-sort


답변

최신 정보

이것이 혼란스러운 사람들이라면 더 나은 글이 있습니다. 몽구스 매뉴얼에서 문서 찾기쿼리 작동 방식을 확인하십시오 . 유창한 API를 사용하려면 find()메소드에 콜백을 제공하지 않고 쿼리 객체를 얻을 수 있습니다 . 그렇지 않으면 아래 개요와 같이 매개 변수를 지정할 수 있습니다.

실물

Modelmodel문서에 따라 객체가 주어지면 다음 과 같이 작동합니다 2.4.1.

Post.find({search-spec}, [return field array], {options}, callback)

search spec객체를 기대한다, 그러나 당신은 통과 할 수 null또는 빈 객체입니다.

두 번째 매개 변수는 문자열 배열 인 필드 목록이므로 ['field','field2']or를 제공 null합니다.

세 번째 매개 변수는 결과 집합을 정렬하는 기능을 포함하는 개체 옵션입니다. 당신이 사용하는 것이 { sort: { field: direction } }어디 field문자열 필드 이름입니다 test(귀하의 경우) 및 direction숫자입니다 1오름차순입니다가와-1 desceding있다가.

마지막 매개 변수 ( 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);
};

HTH


답변

이것이 mongoose.js 2.0.4에서 작동하는 방법입니다.

var query = EmailModel.find({domain:"gmail.com"});
query.sort('priority', 1);
query.exec(function(error, docs){
  //...
});