4 回答
TA贡献1784条经验 获得超2个赞
MongoDB 2.2的新$elemMatch投影操作符提供了另一种方法来更改返回的文档以仅包含第一个匹配的shapes元素:
db.test.find(
{"shapes.color": "red"},
{_id: 0, shapes: {$elemMatch: {color: "red"}}});返回:
{"shapes" : [{"shape": "circle", "color": "red"}]}在2.2中,您也可以使用$ projection operator,其中$投影对象字段名称表示字段中查询的第一个匹配数组元素的索引。以下返回与上面相同的结果:
db.test.find({"shapes.color": "red"}, {_id: 0, 'shapes.$': 1});MongoDB 3.2更新
从3.2版本开始,您可以使用新的$filter聚合运算符在投影期间过滤数组,这样可以包含所有匹配,而不仅仅是第一个匹配。
db.test.aggregate([
// Get just the docs that contain a shapes element where color is 'red'
{$match: {'shapes.color': 'red'}},
{$project: {
shapes: {$filter: {
input: '$shapes',
as: 'shape',
cond: {$eq: ['$$shape.color', 'red']}
}},
_id: 0
}}])结果:
[
{
"shapes" : [
{
"shape" : "circle",
"color" : "red"
}
]
}]TA贡献1712条经验 获得超3个赞
MongoDB 2.2+中的新聚合框架提供了Map / Reduce的替代方案。该$unwind操作可用于分离的shapes阵列到的文件流可以匹配:
db.test.aggregate(
// Start with a $match pipeline which can take advantage of an index and limit documents processed
{ $match : {
"shapes.color": "red"
}},
{ $unwind : "$shapes" },
{ $match : {
"shapes.color": "red"
}})结果是:
{
"result" : [
{
"_id" : ObjectId("504425059b7c9fa7ec92beec"),
"shapes" : {
"shape" : "circle",
"color" : "red"
}
}
],
"ok" : 1}TA贡献1883条经验 获得超3个赞
另一种有趣的方法是使用$ redact,这是MongoDB 2.6的新聚合功能之一。如果您使用的是2.6,则不需要$ unwind,如果您有大型数组,可能会导致性能问题。
db.test.aggregate([
{ $match: {
shapes: { $elemMatch: {color: "red"} }
}},
{ $redact : {
$cond: {
if: { $or : [{ $eq: ["$color","red"] }, { $not : "$color" }]},
then: "$$DESCEND",
else: "$$PRUNE"
}
}}]);$redact “根据文件本身存储的信息限制文件的内容”。所以它只会在文档内部运行。它基本上扫描你的文档顶部到底部,并检查它是否与你的if条件匹配$cond,如果有匹配,它将保留content($$DESCEND)或remove($$PRUNE)。
在上面的示例中,首先$match返回整个shapes数组,$ redact将其删除到预期结果。
请注意,这{$not:"$color"}是必要的,因为它也将扫描顶层文档,如果在顶层$redact找不到color字段,则返回false可能会删除我们不想要的整个文档。
- 4 回答
- 0 关注
- 3545 浏览
添加回答
举报
