查询文档
在MongoDB中,db.collection.find()方法从集合中检索文档。[1] db.collection.find()方法返回一个游标来检索文档。
本教程提供了使用db.collection.find()方法在mongo shell读操作的例子。在这些例子中,检索文档包含它们的所有字段。限制从检索的文档返回的字段,查看限制字段从查询返回。
[1] db.collection.findOne()方法也执行读取返回一个单一的文档的操作。在内部,db.collection.findOne()方法是用db.collection.find()方法返回一个。
一、选择在一个集合中的所有文档
一个空的文档查询({}) 选择集合中的所有文档:
db.inventory.find( {} )
不指定查询文档的find()方法等同于指定一个空的查询文档。因此,下面的操作等效于前面的操作:
db.inventory.find()
二、指定匹配条件
指定匹配条件,用查询文档 { <field>: <value> }选择包含具有指定值<field>: <value>的所有文档。
下面的示例检索inventory集合类型字段的值为小吃的所有文档:
db.inventory.find( { type: "snacks" } )
三、指定条件下使用查询运算符
在一个MongoDB查询中查询文档可以使用查询运算符来指定条件,下面的示例选择inventory集中类型字段的值是'食品'或'小吃'的所有文档:
db.inventory.find( { type: { $in: [ 'food', 'snacks' ] } } )
虽然你可以使用$or操作符表达这个查询,在同一字段进行匹配的检查时,使用运算符$in,而不是$or操作。
参考操作文档的查询操作符的完整列表。
四、指定AND条件
复合查询可以对集合文档中的多个字段指定条件。无疑,一个逻辑AND连接词连接复合查询的子句,以便查询选择符合所有条件的在集合中的文档。
在以下示例中,查询文档指定一个相等匹配在type字段和小于($lt)比较匹配在价格字段。
db.inventory.find( { type: 'food', price: { $lt: 9.95 } } )
这个查询选择其中类型字段的值为“食物”和价格字段的值小于9.95的所有文档。查看其他的比较操作运算符。
五、指定OR条件
使用$OR操作,您可以使用逻辑或结合每个指定的连接子句,以便查询选择在集合中至少匹配一个条件的文档的复合查询。
在下面的示例中,查询文档选择集合中qty字段值大于($GT)100或者价格字段的值的小于($LT)9.95的所有文档。
db.inventory.find(
{
$or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]
}
)
六、指定AND以及OR条件
与其他子句,可以指定具体条件匹配的文档。
在以下示例中,复合查询文档选择集合中的type字段的值是“食物”,并且qty字段要值大于($GT)100或price字段的值小于($LT)9.95的所有文档:
db.inventory.find(
{
type: 'food',
$or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]
}
)
七、嵌入式文档
当字段包含嵌入的文档,查询杨可以在一个嵌入式文档上指定一个精确的匹配,或者在嵌入式文档里使用点表示法指定匹配一个独特的字段。
在嵌入式文档上完全匹配
要指定对整个嵌入文档的相等匹配,使用查询文档{ <field>: <value> }其中<value>是相匹配的文档。在嵌入式文档上相等匹配要求指定的value精确匹配,包括字段顺序。
在以下示例中, 查询匹配所有文档的producer字段的值是一个嵌入式文档仅包括值为"'ABC123"的company字段和值为"123 Street"的address字段。
db.inventory.find(
{
producer:
{
company: 'ABC123',
address: '123 Street'
}
}
)
相等匹配嵌入式文档中的字段
通过使用点表示法对嵌入文档的特定字段进行匹配。选择在一个集合中相等匹配嵌入式文档指定字段的文档,其嵌入式文档包含指定值的指定字段。嵌入文档可以包含额外的字段。
在以下示例中,查询使用点表示法匹配producer字段的值是一个嵌入式文档并且包含一个company字段的值为"ABC123",另外可能还有其他字段的所有文档。
db.inventory.find( { 'producer.company': 'ABC123' } )
八、数组
当字段包含一个数组,在一个数组里你能查询一个精确匹配的数组值。如果数组包含内嵌文档,你可以使用点标记法查询内嵌文档的特定字段。
如果您使用$elemMatch操作符指定多个条件,数组必须包含满足所有条件中的至少一种元素。请参阅单一元素满足条件。
如果您没使用$elemMatch操作符指定多个条件,数组元素的某种组合,不一定是单个元素,必须满足所有条件;例如在数组中,不同的元素可满足条件的不同部分。参元素满足条件的组合。
考虑包括下列文档的inventory集合。
{ _id: 5, type: "food", item: "aaa", ratings: [ 5, 8, 9 ] }
{ _id: 6, type: "food", item: "bbb", ratings: [ 5, 9 ] }
{ _id: 7, type: "food", item: "ccc", ratings: [ 9, 5, 8 ] }
完全匹配一个数组
在一个数组上指定相等匹配,使用查询文档{ <field>: <value> } 其中<value>是相匹配的数组。相等匹配数组要求该数组字段完全匹配指定的值<value>,包括元素的顺序。
下面的示例查询rating字段是一个数组,仅有5,8,9这样排序的三个元素的所有文档。
db.inventory.find( { ratings: [ 5, 8, 9 ] } )
这个操作返回下面的文档:
{ "_id" : 5, "type" : "food", "item" : "aaa", "ratings" : [ 5, 8, 9 ] }
匹配一个数组元素
相等匹配可以指定数组,以匹配一个单一的元素。
这些规格相匹配,如果数组使用指定的值至少包含一个元素。
下面的示例查询其中rating包含5作为其内容之一的数组的所有文档:
db.inventory.find( { ratings: 5 } )
操作返回下列文档:
{ "_id" : 5, "type" : "food", "item" : "aaa", "ratings" : [ 5, 8, 9 ] }
{ "_id" : 6, "type" : "food", "item" : "bbb", "ratings" : [ 5, 9 ] }
{ "_id" : 7, "type" : "food", "item" : "ccc", "ratings" : [ 9, 5, 8 ] }
匹配一个数组的特定元素
相等匹配可以指定一个特定的索引处的元素的相等匹配,或者使用点标记数组的位置。
在以下示例中,查询使用点标记匹配其中评级数组包含第一个元素为5的所有文件:
db.inventory.find( { 'ratings.0': 5 } )
操作返回下列文档:
{ "_id" : 5, "type" : "food", "item" : "aaa", "ratings" : [ 5, 8, 9 ] }
{ "_id" : 6, "type" : "food", "item" : "bbb", "ratings" : [ 5, 9 ] }
为数组元素指定多准则
单元素满足条件
使用$elemMatch操作符在一个数组元素上指定多个准则使得至少一个数组元素满足所有指定条件。
下面的示例查询所在的评级数组至少包含一个元素大于($GT)5且小于($LT)9的文档:
db.inventory.find( { ratings: { $elemMatch: { $gt: 5, $lt: 9 } } } )
操作返回下列文档,其评级数组包含的元素8,符合标准:
{ "_id" : 5, "type" : "food", "item" : "aaa", "ratings" : [ 5, 8, 9 ] }
{ "_id" : 7, "type" : "food", "item" : "ccc", "ratings" : [ 9, 5, 8 ] }
元素满足条件的组合
下面的示例查询的文档所在的评级数组包含的元素,在某些组合满足查询条件;例如,一个元素能满足大于5的条件,另一个元素满足小于9的条件。或者两者都满足。
db.inventory.find( { ratings: { $gt: 5, $lt: 9 } } )
操作返回下面的文档:
{ "_id" : 5, "type" : "food", "item" : "aaa", "ratings" : [ 5, 8, 9 ] }
{ "_id" : 6, "type" : "food", "item" : "bbb", "ratings" : [ 5, 9 ] }
{ "_id" : 7, "type" : "food", "item" : "ccc", "ratings" : [ 9, 5, 8 ] }
文档用"ratings" : [ 5, 9 ]匹配查询,9满足大于5的条件(第一个条件),5满足小于9的条件(第二个条件)。
内嵌文档数组
考虑清单集合包含下面的文档:
{
_id: 100,
type: "food",
item: "xyz",
qty: 25,
price: 2.5,
ratings: [ 5, 8, 9 ],
memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}
{
_id: 101,
type: "fruit",
item: "jkl",
qty: 10,
price: 4.25,
ratings: [ 5, 9 ],
memos: [ { memo: "on time", by: "payment" }, { memo: "delayed", by: "shipping" } ]
}
用数组索引在内嵌文档里匹配一个字段
如果您知道嵌入文档的数组索引,您可以使用点标记法指定使用子文档的位置的文档。
以下示例选择所在的memos包含数组的第一个元素(即索引为0)是包含字段值为“shipping”的文档的所有文档:
db.inventory.find( { 'memos.0.by': 'shipping' } )
操作返回以下文档:
{
_id: 100,
type: "food",
item: "xyz",
qty: 25,
price: 2.5,
ratings: [ 5, 8, 9 ],
memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}
不指定数组索引匹配一个字段
如果您不知道数组中的文档的索引位置,串联包含数组中字段的名称,以点(.)和字段的名称的子文档。
下面的示例选择memos字段为至少包含一个内嵌文档的by字段值为“shipping”的数组的所有文档:
db.inventory.find( { 'memos.by': 'shipping' } )
操作返回下列文档:
{
_id: 100,
type: "food",
item: "xyz",
qty: 25,
price: 2.5,
ratings: [ 5, 8, 9 ],
memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}
{
_id: 101,
type: "fruit",
item: "jkl",
qty: 10,
price: 4.25,
ratings: [ 5, 9 ],
memos: [ { memo: "on time", by: "payment" }, { memo: "delayed", by: "shipping" } ]
}
指定文档数组的多个条件
单元素满足条件
使用$elemMatch操作符在嵌入文档数组上指定多个条件使得至少一个嵌入文档满足所有指定的条件。
下面的示例查询的文档其memos数组至少包括一个包含两个字段的内嵌的文档,memo字段的值为“on tim”,by字段的值为“shipping”:
db.inventory.find(
{
memos:
{
$elemMatch:
{
memo: 'on time',
by: 'shipping'
}
}
}
)
操作返回以下文档:
{
_id: 100,
type: "food",
item: "xyz",
qty: 25,
price: 2.5,
ratings: [ 5, 8, 9 ],
memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}
元素满足条件的组合
下面的示例查询的文档所在的memos数组包含的元素,在某些组合满足查询条件;例如一个元素字段memo等于“on time”条件,另一个元素字段by等于“shipping”,或一个元素两个条件都满足。
db.inventory.find(
{
'memos.memo': 'on time',
'memos.by': 'shipping'
}
)
该查询返回下列文档:
{
_id: 100,
type: "food",
item: "xyz",
qty: 25,
price: 2.5,
ratings: [ 5, 8, 9 ],
memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}
{
_id: 101,
type: "fruit",
item: "jkl",
qty: 10,
price: 4.25,
ratings: [ 5, 9 ],
memos: [ { memo: "on time", by: "payment" }, { memo: "delayed", by: "shipping" } ]
}