如何查询嵌套对象? [英] How to query nested objects?

查看:48
本文介绍了如何查询嵌套对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用嵌套对象表示法查询mongoDB时遇到问题:

I have a problem when querying mongoDB with nested objects notation:

db.messages.find( { headers : { From: "reservations@marriott.com" } } ).count()
0
db.messages.find( { 'headers.From': "reservations@marriott.com" }  ).count()
5

我看不到我在做什么错.我期望嵌套对象表示法返回与点表示法查询相同的结果.我在哪里错了?

I can't see what I am doing wrong. I am expecting nested object notation to return the same result as the dot notation query. Where am I wrong?

推荐答案

db.messages.find( { headers : { From: "reservations@marriott.com" } } )

此查询headers 等于 { From: ... },即不包含其他字段的文档.

This queries for documents where headers equals { From: ... }, i.e. contains no other fields.

db.messages.find( { 'headers.From': "reservations@marriott.com" } )

这仅查看headers.From字段,不受headers中包含或缺少的其他字段的影响.

This only looks at the headers.From field, not affected by other fields contained in, or missing from, headers.

点符号文档

这篇关于如何查询嵌套对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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