如何查询嵌套对象? [英] How to query nested objects?
问题描述
使用嵌套对象表示法查询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屋!