最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Returning specific fields with mongoose - Stack Overflow

programmeradmin1浏览0评论

I'm trying to accomplish something really easy but still manage to fail.

What I am trying to do is when I get a get request on my server I want to return all documents BUT just the specific fields populated.

My schema goes as follows

var clientSchema = new Schema({
    name:{
        type: String,
        required: true
    },
    phone:{
        type: String,
        required: true
    },
    email:{
        type: String,
        required: true
    },
    address: {
        type: String,
        required: false
    }
});

var orderDetailsSchema = new Schema({
    //isn't added to frontend
   confirmed:{
       type: Boolean,
       required: true,
       default: false
   },    
   service:{
       type: String,
       required: true
   },
   delivery:{
       type: String,
       required: false
   },
    payment:{
        type: String,
        required: false
    },
    status:{
        type: String,
        required: true,
        default: "new order"
    },
});

var orderSchema = new Schema({

   reference:{
       type: String,
       required: true
   },

    orderdetails: orderDetailsSchema,

    client: clientSchema,

    wheelspec: [wheelSchema],

    invoice:{
        type: Schema.Types.ObjectId,
        ref: 'Invoice'
    }


});

What I want is to return only client.phone and client.email plus orderdetails.status but still retain reference field if possible

I have tried using lean() and populate() but had no luck with them. Is there anything utterly simple I am missing? Or what I am trying to achieve is not that easy? Thanks!

I'm trying to accomplish something really easy but still manage to fail.

What I am trying to do is when I get a get request on my server I want to return all documents BUT just the specific fields populated.

My schema goes as follows

var clientSchema = new Schema({
    name:{
        type: String,
        required: true
    },
    phone:{
        type: String,
        required: true
    },
    email:{
        type: String,
        required: true
    },
    address: {
        type: String,
        required: false
    }
});

var orderDetailsSchema = new Schema({
    //isn't added to frontend
   confirmed:{
       type: Boolean,
       required: true,
       default: false
   },    
   service:{
       type: String,
       required: true
   },
   delivery:{
       type: String,
       required: false
   },
    payment:{
        type: String,
        required: false
    },
    status:{
        type: String,
        required: true,
        default: "new order"
    },
});

var orderSchema = new Schema({

   reference:{
       type: String,
       required: true
   },

    orderdetails: orderDetailsSchema,

    client: clientSchema,

    wheelspec: [wheelSchema],

    invoice:{
        type: Schema.Types.ObjectId,
        ref: 'Invoice'
    }


});

What I want is to return only client.phone and client.email plus orderdetails.status but still retain reference field if possible

I have tried using lean() and populate() but had no luck with them. Is there anything utterly simple I am missing? Or what I am trying to achieve is not that easy? Thanks!

Share Improve this question asked May 24, 2017 at 14:53 Jack WoltherJack Wolther 1311 gold badge2 silver badges12 bronze badges 2
  • 1 what your get call looks like? you can use mongodb projection – Atish Commented May 24, 2017 at 14:58
  • need to see your current query – Alex Commented May 24, 2017 at 15:12
Add a comment  | 

3 Answers 3

Reset to default 9

You can specify the fields to return like this:

Order.findOne({'_id' : id})
        .select('client.phone client.email orderdetails.status reference')
        .exec(function(err, order) {
        //
});

Alternative syntax

Order.findOne({'_id' : id})
    .select('client.phone client.email orderdetails.status reference')
    .exec(function(err, order) {
      //
});

I've made a number of assumptions here, but you should be able to see the idea.

Simply do like this :-

Order is model name which is registered in mongoose.

Order.findById(id) // set id you have to get 
   . populate('client')
    .select('client.phone client.email orderdetails.status reference')
    .exec(function(err, order) {
      //
});

You can use projection.

await Order.findById(diaryId, {phone: 1, email: 1, status: 1})

If phone:1 is set to 1, it is included, if phone:0, then it's excluded.

发布评论

评论列表(0)

  1. 暂无评论