복붙노트

[MONGODB] 노드 - 몽구스 3.6 - 인구 필드 정렬 쿼리

MONGODB

노드 - 몽구스 3.6 - 인구 필드 정렬 쿼리

그래서 내가 모든 필드를 정렬 (오름차순, 내림차순)을 처리해야합니다, 원격 그리드 사용하는 쿼리를 수행하려합니다.

다음은 스키마는 다음과 같습니다

var customerSchema = new mongoose.Schema({
status: {type: mongoose.Schema.Types.ObjectId, ref: 'Status'},
contact: {type: mongoose.Schema.Types.ObjectId, ref: 'Contact'}
}, { collection: 'Customer' });

customerSchema.virtual('contactName').get(function () {
   if (this.contact && this.contact.get) {
       return this.contact.get('firstName') + ' ' + this.contact.get('lastName');
   }

   return '';
});

customerSchema.virtual('statusName').get(function () {
   if (this.status && this.status.get) {
       return this.status.get('name');
   }

   return '';
});

customerSchema.set('toJSON', { virtuals: true });
customerSchema.set('toObject', { virtuals: true });
mongoose.model('Customer', customerSchema);

// STATUS
var statusSchema = new mongoose.Schema({}, { collection: 'Status' });
mongoose.model('Status', statusSchema);

// CONTACT
var contactSchema = new mongoose.Schema({
    firstName: String,
    lastName: String
}, { collection: 'Contact' });
mongoose.model('Contact', contactSchema);

그리고 여기 쿼리는 다음과 같습니다

exports.customerList = function (predicate ,callback){
if (!predicate) predicate = 'name';
var Customers = mongoose.model( 'Customer' );

Customers.find()
    .select('name phone address status contact contactName statusName')
    .populate('status', 'name')
    .populate('contact', 'firstName lastName')
    .sort(predicate)
    .exec(callback);
};

쿼리는 '이름'(Customer.name 정도) 또는 '주소'(Customer.address)에 정렬 할 때 노력하고 있지만 'contact.firstName'(Customer.contact.firstName해야한다) 인 경우 작업에 그것을 얻을 캔트.

채우기의 fonction의 네 번째 매개 변수는 옵션 정렬 객체를 가질 수 느릅 나무 개체하지만,이 일을 수 있습니다 :

.populate('contact', 'firstName lastName', null, { sort {'firstName': 1}})

작동하지 않는 경우 (고객의 연락처 목록을 정렬하는 것).

나는 몽구스 (그리고 몽고)에 완전히 새로운입니다. 나는 레일 노드에 프로젝트 포트 노력하고 있어요 / 표현한다.

내가 contact.firstName에 의해 내 쿼리를 정렬 할 수있는 방법이 있나요?

감사!

편집 : 내 종류의 수동 (에 Array.sort를)하고 결국하지만 난 정말이 솔루션처럼 해달라고. (내가 틀렸다면 올바른 나)는 Node.js를 메인 스레드를 차단 있도록 정렬 동기입니다.

내가 이해하지 못하는 뭔가가 있나요? 데이터 집합을 정렬하면 나를 위해 데이터베이스 문제가 아닌 응용 프로그램입니다 ... 제가 Node.js를에 내 레일에게 응용 프로그램을 전환에 대한 희망을 많이 얻고 있었다 있지만 구현하기 위해 정말 열심히 일부 표준 작업 (그리드 페이징) 것처럼 보인다!

해결법

  1. ==============================

    1.당신은 일종의 가상 필드 또는 인구 필드에 해당 필드가 앱 객체 (몽구스 모델의 경우)에만 존재하지 수 있지만 종류 MongoDB를 내에서 실행됩니다.

    당신은 일종의 가상 필드 또는 인구 필드에 해당 필드가 앱 객체 (몽구스 모델의 경우)에만 존재하지 수 있지만 종류 MongoDB를 내에서 실행됩니다.

    이 MongoDB의 결과가 지원하지 않는 것이 조인 키 제한 중 하나입니다. 데이터가 매우 관계형 인 경우 대신하여 MongoDB의 관계형 데이터베이스를 사용하는 것이 좋습니다.

  2. from https://stackoverflow.com/questions/19428471/node-mongoose-3-6-sort-query-with-populated-field by cc-by-sa and MIT license