[英]Dot notation on nested objects not in Mongoose schema
I have a mongoose schema with a providerData object: 我有一个带有providerData对象的猫鼬模式:
... , providerData: {}, ...
and I would like to query for documents based on the IDs of objects in the providerData
object. 我想根据providerData
对象中对象的ID查询文档。
So I assumed I would have to use dot notation like so: 所以我认为我必须像这样使用点符号:
User.findOne({ providerIDString: providerID }, function(err, user) {...});
where 哪里
providerIDString is a string like 'providerData.facebook.id'
providerID is the providerData.facebook id
However the query keeps returning no results even though 但是,即使查询仍然没有返回结果
db.users.find({"providerData.facebook.id":"THEFACEBOOKID"})
in the mongodb shell returns the correct document 在mongodb shell中返回正确的文档
Am I correct to assume this is happening because providerData.facebook
and providerData.facebook.id
are not defined in the User schema? 我是否正确假设发生这种情况,因为在User模式中未定义providerData.facebook
和providerData.facebook.id
?
Does this mean I have to add them to the schema or is there a way to use dot notation on nested objects not in the mongoose schema? 这是否意味着我必须将它们添加到架构中,还是有办法在非猫鼬架构中的嵌套对象上使用点符号?
I'm guessing that you have input like: 我猜测您输入的内容如下:
var providerIdString = 'providerData.facebook.id',
facebookId = "THEFACEBOOKID";
In which case you use: 在这种情况下,请使用:
var query = {};
query[provoderIdString] = facebookId;
Then: 然后:
db.users.find(query);
And behold! 瞧! It works. 有用。
That is how you construct objects from variable names in JavaScript. 这就是从JavaScript中的变量名构造对象的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.