Mongo: find subdocument without dot notation

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

For example we have collection

{field: {subfield: 'name'}} {field: {subfield: 'phone'}} 

Can I find document without dot notation? Like this

db.test.find({field: {subfield: /regex/}}) 

or maybe like this

db.test.find({field: {$someOperator: {subfield: /regex/}}}) 

I just don't want to build dot notation like

db.test.find({"field.subfield": /regex/}) 

回答1:

The problem is that:

db.test.find({field: {$someOperator: {subfield: /regex/}}}) 

Is actually another way of querying in MongoDB which uses object euqality to search for subdocuments.

So no, you must use dot notation unless you were searching for where one object exactly equals the other.

That being said you could wrap the document in $elemMatch: http://docs.mongodb.org/manual/reference/operator/elemMatch/ that would work

Edit

Considering you collection structure $elemMatch won't actually work.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!