Reputation: 13
When I use return await Model.save()
they return a lot of data. There is a way to return the document only?
Code:
async (data) =>{
const charData = new MyModel({
steamId: data.steam,
phone: data.phone,
registration: data.registration,
})
return await MyModel.save()
}
I've searched in many websites but I didnt find any example using async functions.
There is a example that MyModel.save()
are returning:
So, I want get only _doc object instead it all when return await MyModel.save()
Upvotes: 1
Views: 2454
Reputation: 2127
Then try like this
let res = await Model.save();
return res._doc;
Upvotes: 0
Reputation: 74670
Document.prototype.toObject()
converts a mongoose document into the plain javascript object representation.
async (data) =>{
const charData = new MyModel({
steamId: data.steam,
phone: data.phone,
registration: data.registration,
})
await charData.save()
return charData.toObject({ getters: true })
}
Note the options which affect how the document is represented:
getters
apply all getters (path and virtual getters), defaults to falsealiases
apply all aliases ifvirtuals=true
, defaults to truevirtuals
apply virtual getters (can override getters option), defaults to falseminimize
remove empty objects, defaults to truetransform
a transform function to apply to the resulting document before returningdepopulate
depopulate any populated paths, replacing them with their original refs, defaults to falseversionKey
whether to include the version key, defaults to trueflattenMaps
convert Maps to POJOs. Useful if you want to JSON.stringify() the result of toObject(), defaults to falseuseProjection
set totrue
to omit fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that hasselect: false
in the schema.
Upvotes: 5