mongodb - 聚合意外令牌:
问题描述
下面聚合 mongodb 查询给出
意外的令牌:错误
db.getCollection("products_data").aggregate(
{
"$unwind": {
"path": "$color",
"preserveNullAndEmptyArrays": true
}
},
{
"$match":{
"country":"UK",
"$or":[{
"$and":[
"$or":
[{
"$and":[
{"status":"drafted"},
{"color":{$in:["blue"]}}
]},
{"$and":[
{"status1":"complete"},
{"status2":{$nin:["n/a","drafted","complete"]}},
{"color":{$in:["green"]}}
]}
]
]
},{
"$and":[
"$or":
[
{ "$and":[
{"status":"drafted"},
{"color":{$in:["blue"]}}
]},
{"$and":[
{"status1":"complete"},
{"status2":{$nin:["n/a","drafted","complete"]}},
{"color":{$in:["green"]}}
]}
]
]
}
]
}
},
{
"$group":{
"_id":"$field",
"products":{$sum: 1},
"bid":{"$push":"$product_id"}
}
},
{
"$project":{
"field":"$_id",
"products":"$products",
"bid":1,
"_id":0
}
}
);
获取给定指定条件的聚合计数。
解决方案
正确使用的语法aggregate
和它的阶段pipeline
db.getCollection("products_data").aggregate([
{ "$unwind": { "path": "$color", "preserveNullAndEmptyArrays": true }},
{ "$match": {
"country": "UK",
"$or": [
{
"$and": [
{
"$or": [
{ "$and": [{ "status": "drafted" }, { "color": { "$in": ["blue"] }}] },
{ "$and": [{ "status1": "complete" }, { "status2": { "$nin": ["n/a", "drafted", "complete"] }}, { "color": { "$in": ["green"] }}]}
]
}
]
},
{
"$and": [
{
"$or": [
{ "$and": [{ "status": "drafted" }, { "color": { "$in": ["blue"] }}] },
{ "$and": [{ "status1": "complete" }, { "status2": { "$nin": ["n/a", "drafted", "complete"] }}, { "color": { "$in": ["green"] }}] }
]
}
]
}
]
}},
{ "$group": {
"_id": "$field",
"products": { "$sum": 1 },
"bid": { "$push": "$product_id" }
}},
{ "$project": { "field": "$_id", "products": "$products", "bid": 1, "_id": 0 }}
])
推荐阅读
- javascript - 每个对象的标签值对象
- google-apps-script - 该范围的坐标在工作表的尺寸之外。谷歌脚本
- python - Altair:带有笔划点标记的折线图
- javascript - 如何处理一个项目中多个相同的类?
- node.js - 在 Node js 中使用 csv-parser 获取文件中的原始行
- python - 检查点是否在多边形内
- python - 输入 0 与层 lstm_34 不兼容:预期 ndim=3,发现 ndim=2
- python - 气流电子邮件操作员成功/失败
- sql - 识别 SQL Server 表中重复行组合的 ID
- java - 在活动之间传递 textview 值