복붙노트

[MONGODB] 몽구스, 발견, 반환 특정 속성

MONGODB

몽구스, 발견, 반환 특정 속성

나는이 GET 호출을 가지고 :

exports.getBIMFromProject = function(req, res){
  mongoose.model('bim').find({projectId: req.params['prj_id']}, function(err, bim){
    if(err){
      console.error(err);
      res.send(500)
    }
    res.send(200, bim);
  });
};

어디 내가 반환 할 속성을 지정합니까? 워드 프로세서에서 찾을 수 없습니다. 위의 반환 전체 개체. 나는 단지 몇 가지 속성을 반환합니다.

이것은 내 스키마입니다 :

var mongoose = require('mongoose'),
  Schema = mongoose.Schema;

var bimSchema = new Schema({
  projectId: Number,
  user: String,
  items:[
    {
      bimObjectId: Number,
      typeId: String,
      position:{
        floor: String,
        room:{
          name: String,
          number: String
        }
      }
    }
  ]
});

mongoose.model('bim', bimSchema);

내 나머지 통화에 포함 된 항목의 배열을 원하지 않는다.

해결법

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

    1.당신은 프로젝션을 사용합니다. 몽구스 질의 문서의 첫 번째 예에서 꿰매 투영 동작을 갖는다.

    당신은 프로젝션을 사용합니다. 몽구스 질의 문서의 첫 번째 예에서 꿰매 투영 동작을 갖는다.

    NB : 나는 트리플 별 중요한 비트를 강조하지 실제 코드 B / C

    // find each person with a last name matching 'Ghost', ***selecting the `name` and `occupation` fields***
    Person.findOne({ 'name.last': 'Ghost' }, ***'name occupation'***, 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.
    })
    

    사람 스키마가 지정되지하지만 난 예를 분명 충분하다 생각합니다.

  2. ==============================

    2.당신은 당신의 모델 스키마 http://mongoosejs.com/docs/guide.html를 정의 할 필요가

    당신은 당신의 모델 스키마 http://mongoosejs.com/docs/guide.html를 정의 할 필요가

  3. from https://stackoverflow.com/questions/25330555/mongoose-find-return-specific-properties by cc-by-sa and MIT license