Mongo:查找不带点符号的子文档 [英] Mongo: find subdocument without dot notation

查看:57
本文介绍了Mongo:查找不带点符号的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我们有收藏

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

我可以找到没有点号的文档吗?像这样

Can I find document without dot notation? Like this

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

或者也许是这样

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

我只是不想建立点符号

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

推荐答案

问题是:

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

实际上是MongoDB中的另一种查询方法,它使用对象等价性搜索子文档.

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.

话虽如此,您可以将文档包装在$elemMatch中: http://会起作用的docs.mongodb.org/manual/reference/operator/elemMatch/

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

考虑到您的收集结构$elemMatch实际上将不起作用.

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

这篇关于Mongo:查找不带点符号的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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