如何获取json对象的数组而不是mongoose文件 [英] How to get array of json objects rather than mongoose documents
本文介绍了如何获取json对象的数组而不是mongoose文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我找到如下操作时:
Collection.find({name: 'Erik'}, function (err, docs) {
// do momething
});
'docs'变量填充有一组功能完备的mongoose文档。但是我需要得到一组纯粹的JSON对象。
'docs' variable is populated with an array of fully functional mongoose documents. But I need to get an array of pure JSON objects.
我知道我可以通过forEach循环遍历'docs'数组,并使用.toJSON()获取对象方法。 mongoose是否支持我感兴趣的功能?
I know I can loop through the 'docs' array by forEach and get an objects by using .toJSON() method. Does mongoose support the feature I'm interested?
推荐答案
如果您使用Mongoose 3.x,您可以使用 lean
查询选项来执行此操作:$($)
If you're using Mongoose 3.x you can use the lean
query option to do this:
Collection.find({name: 'Erik'}).lean().exec(function (err, docs) {
// docs are plain javascript objects instead of model instances
});
这篇关于如何获取json对象的数组而不是mongoose文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文