猫鼬,查找,返回特定属性 [英] Mongoose, find, return specific properties

查看:74
本文介绍了猫鼬,查找,返回特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个电话:

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

我在哪里指定要返回的属性?在文档中找不到它.上面的返回了整个对象.我只想返回一些属性.

Where do I specify which properties I want to return? Can't find it in the docs. The above returns the entire object. I only want a few properties returned.

这是我的模式:

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);

我不想在我的休息电话中包含项目数组.

I don't want the items array included in my rest call.

推荐答案

您使用投影. mongoose查询文档中的第一个示例具有投影操作.

You use projection. The first example in the mongoose query docs has a projection operation tucked in.

注意:不是真正的代码b/c,我用三颗星突出显示了重要的部分

NB: not real code b/c I highlighted the important bits with triple stars

// 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.
})

未指定Person模式,但我认为示例足够清楚.

The Person schema isn't specified but I think the example is clear enough.

这篇关于猫鼬,查找,返回特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆