我有一个集合,其中投资是 mongodb 文档内的一个数组。现在使用聚合,我尝试过滤投资长度超过 5 倍的结果,然后使用匹配查询进行下一步处理。
Collection{
_id:000000
---------------------
"investments" : [ {
hhhhhhhhhhhhhh
},
{
hhhhhhhhhhhhhh
} }]
-----------------
我写的匹配查询如下所示,但不起作用。有什么建议么:
db.companies.aggregate( [
{ $match: {"founded_year" : 2004},
{ "investments" : {$size: : { $gte: 5 } } } },
----------------------------------
--------------------------------
]}
With aggregate
:
db.companies.aggregate([
{ $match: { "founded_year":2004 } },
{ $project: { founded_year:1,
moreThanFive: { $gt: [ {$size: "$external_links" }, 5 ] } } },
{ $match: { moreThanFive : true }} ,
])
您将需要:
1. 包括一个$project
阶段,找出投资数量(size
数组的),并检查是否大于 5。
2.然后再做一次$match
阶段来过滤那些moreThanFive
等于true
.
With find
:
db.companies.find({'investments.5': {$exists: true}})
你问是否位置号6在investments
数组存在。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)