mongoose .find()方法返回具有不需要的属性的对象 [英] mongoose .find() method returns object with unwanted properties

查看:639
本文介绍了mongoose .find()方法返回具有不需要的属性的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经和猫鼬一起工作了一段时间,我发现了一些非常奇怪的东西。如果有人可以启发我会很棒。

so, I've been working with mongoose for some time and I found some really weird stuff going on. It would be great if someone could enlighten me.

事实是,当使用mongoose的.find()方法时,我得到的对象充满属性我不知道它来自哪里(我猜它们是内置属性,但无论如何)我只想通过属性I .select()进行迭代。得到它了?没有?好的...解释得更好:

The thing is, when using the .find() method of mongoose, the object I get as response is full of properties I don't know where It came from (I'm guessing they are built-in properties, but whatever) and I want to iterate only through the properties I .select(). Got it? No? ok... explaining better:

我宣布了我的架构和模型:

I have my schema and model declared:

var mySchema = mongoose.Schema({
  name: String,
  prop1: String,
  prop2: String,
  prop3: String
})
var myModel = DB.model('myDataBase', mySchema)

然后我想找一个带有名字的文件,比方说John,并检索除名称字段以外的所有文件,所以我去了:

Then I want to find a document with the name, let's say, John and retrieve all but the 'name' field, so I go:

myModel.find({name: 'John'}, '-name', function(err, results){
  log(results[0])
}

和log(结果[0])日志

and log(results[0]) logs

{ prop1: 'one',
  prop2: 'two',
  prop3: 'three' }

到目前为止,这么好。但问题是,现在我想迭代这些属性并逐个检查,我不确定每个结果会有多少'道具',所以我想做类似的事情:

So far, so good. But the problems is, now I want to iterate through these properties and check one by one, and I don't know for sure how many 'props' each result will have, so I wanted to do something like:

for(var key in results[0]){
  log(key)
}

所以,我希望它会记录'prop1','prop2'和'prop3',但不是!好吧,我得到道具1,2和3,但我也得到了很多其他的属性和功能,如:isNew,error,_maxListeners,_doc等。不仅这些额外属性,我还得到'name'属性,我从选择中排除了一个(并且它被排除在外,如第一个日志中所示)。怪啊?

So, I'm hoping it will log 'prop1', 'prop2' and 'prop3', but no! Ok, I get props 1, 2 and 3, but also I get a lots of other properties and functions like: isNew, error, _maxListeners, _doc, etc. Not only these extras properties, I also get the 'name' property, the one I excluded from the selection (and it was excluded, like shown in the first log). Weird huh?

但是等等!还有更多!我在网上搜索过,发现有些人说Dude,迭代对象属性时使用hasOwnProperty方法!。所以我去了:

But wait! There's more! I've searched online and found some people saying "Dude, when iterating through object properties use the hasOwnProperty method!". So there I went:

for (var key in results[0]){
  if (results[0].hasOwnProperty(key)) log(key)
}

日志结果是几个属性(具体来说:$ __,isNew,error,_maxListeners,_doc,_pres,_posts,save,_events)并且不包括我想要的任何道具。

the log result is a few properties (to be specific: $__, isNew, error, _maxListeners, _doc, _pres, _posts, save, _events) and doesnt include any of the props I wanted in the first place.

我的问题是,如何只迭代道具1,2和3,排除这些,我不知道,内置属性和我在参数中明确排除的属性? (ps:我在想一个不需要将我的对象转换成数组的解决方案,如果可能的话)

My question is, how can I iterate through only prop 1, 2 and 3, excluding these, I don't know, built-in properties and the one I explicitly excluded in the parameters? (ps: I was thinking of a solution that doesnt involve having to convert my object into an array, if thats possible)

此外,这不是一个问题本身,而是好奇心,这些属性来自哪里?为什么它们出现在for循环中而不是在我记录对象时?为什么我排除的属性('-name')也出现在for循环中?如果它没有识别刚刚记录的属性,那么还有什么是onOwnProperty?

Also, not a question per se, but for curiosity, where does these properties come from? Why do they appear in the for loop and not when I log the object? Why the property I excluded ('-name') also appears in the for loop? What the hell is hasOwnProperty for if it doesnt recognize the properties that were just logged?

感谢您的时间和帮助!
再见!

Thanks for your time and help! Bye!

推荐答案

除了Kevin B的答案,你可以传递 {lean:true } 作为选项:

Alternatively to Kevin B's answer, you can pass {lean: true} as an option:

myModel.find({name: 'John'}, '-name', {lean: true}, function(err, results){
  log(results[0])
}

在MongoDB中,文档只是作为对象保存。当Mongoose检索它们时,它会将它们转换为Mongoose文档。这样做会添加所有那些包含在<$ $中的键。 c $ c> for loop。这是允许你使用所有文档方法的。如果你不使用任何这些, lean 是一个很好的选择,因为它会跳过整个过程,提高查询速度。可能快3倍。

In MongoDB, the documents are saved simply as objects. When Mongoose retrieves them, it casts them into Mongoose documents. In doing so it adds all those keys that are being included in your for loop. This is what allows you to use all the document methods. If you won't be using any of these, lean is a great option as it skips that entire process, increasing query speed. Potentially 3x as fast.

这篇关于mongoose .find()方法返回具有不需要的属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆