Mongo: find subdocument without dot notation

前端 未结 1 421
小鲜肉
小鲜肉 2021-01-17 23:58

For example we have collection

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

Can I find document without dot notation? Like

相关标签:
1条回答
  • 2021-01-18 00:19

    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.

    0 讨论(0)
提交回复
热议问题