Select fields bringing more fields than expected

9 views
Skip to first unread message

David Rissato Cruz

unread,
Oct 9, 2015, 5:16:34 PM10/9/15
to Mongoose Node.JS ODM
I don't know if this is a mongoose or a mongo issue, but running this example:

var mongoose = require('mongoose');

mongoose
.connect("mongodb://localhost:27017/test");

var sampleSchema = new mongoose.Schema({
    _id
: Number,
    name
: String,
    approval
: {
        approved
: {type: Boolean, default: false},
        approver
: String
   
}
});

var Sample = mongoose.model("Sample", sampleSchema);


Promise
   
.all([
       
(new Sample({_id: 1, name: "test1"})).save(),
       
(new Sample({_id: 2, name: "test2"})).save(),
       
(new Sample({_id: 3, name: "test3", approval: {approver: "David"}})).save()
   
])
   
.then(function () {
       
return Sample.find().select("_id").exec(); // ** WE WANT _id FIELD ONLY
   
})
   
.then(console.log)
   
.catch(console.error)
   
.then(mongoose.disconnect.bind(mongoose));



Returns an array like that:

[ { _id: 1, approval: {} },
 
{ _id: 2, approval: {} },
 
{ _id: 3, approval: {} } ]




Where I expected that:

[ { _id: 1},
 
{ _id: 2},
 
{ _id: 3} ]




Does anybody know why mongoose behave like that? Isn't it a bug?
Reply all
Reply to author
Forward
0 new messages