嗨:我使用了多个$match和$and运算符,我得到了错误:
uncaught exception: aggregate failed: {
"errmsg" : "exception: Unrecognized pipeline stage name: '$and'",
"code" : 16436,
"ok" : 0
}我的db.version返回3.0.4。根据MongoDb文档,这应该在版本3.0.x中得到支持。这里有什么地方我做错了吗?谢谢你的帮助。
下面是我的问题:
db.getCollection('plannedVoyageStops').aggregate(
{$and : {$match:{ "scenarioId":"xxxx"},$match:{"action":"remove"}} },
{$unwind : "$plannedCMSs" },
{$project: {_id: 0, scenarioId: 1,
"shipmentNumber":"$plannedCMSs.shipmentNumber",
"vsId": 1, "stopSeqNum":1, "trade":1,
"svvd": 1,
"action":"$plannedCMSs.action"
}
}
);单个匹配条件可以很好地工作,并且结果集合包含所有类型的操作,理想情况下,我打算只获取那些带有action=="remove“的操作。
# db.getCollection('plannedVoyageStops').aggregate(
# {$match:{ "scenarioId":"xxxx"}},
# {$unwind : "$plannedCMSs" },
# {$project: {_id: 0, scenarioId: 1,
# "shipmentNumber":"$plannedCMSs.shipmentNumber",
# "vsId": 1, "stopSeqNum":1, "trade":1,
# "svvd": 1,
# "action":"$plannedCMSs.action"
# }
# }
# );发布于 2016-01-15 08:36:42
您的问题是您拥有两次$match密钥。这是不允许的。如果希望在$and运算符中匹配两个条件,则需要一个条件数组。但是,这里不需要$and:在mongo查询中,$and是隐式的,只使用逗号。只需将JSON查询分配给您的$match:
db.getCollection('plannedVoyageStops').aggregate(
{$match : { "scenarioId":"xxxx","action":"remove"}},
{$unwind : "$plannedCMSs" },
{$project: {_id: 0, scenarioId: 1,
"shipmentNumber":"$plannedCMSs.shipmentNumber",
"vsId": 1, "stopSeqNum":1, "trade":1,
"svvd": 1,
"action":"$plannedCMSs.action"
}
}
);https://stackoverflow.com/questions/34801969
复制相似问题