Reputation: 65
I tried "_id": "$field"
, but it doesn't work.
"_id": 0
removes the _id field entirely
Upvotes: 0
Views: 57
Reputation: 1426
using mongoose
var UserSchema = new mongoose.Schema({
//other fields
},{
minimize: false,
toJSON: {
transform: function (doc, ret) {
ret.newField= ret._id;
delete ret._id;
}
}
});
and query like model.find()
using aggregation
model.aggregate([
{
$match:{}
},
{
$project:{
"newField":"$_id",
"_id":0
}
}
])
Upvotes: 1