Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo: find subdocument without dot notation

Tags:

mongodb

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/})
like image 995
redexp Avatar asked Apr 19 '13 12:04

redexp


1 Answers

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.

like image 93
Sammaye Avatar answered Oct 11 '22 05:10

Sammaye