MongoDB 未知的顶级运算符

Ren*_*ues 0 mongodb mongodb-query

好吧,我在 mongo 的聚合方面遇到了问题,我需要验证是否在所有答案中都没有标记为 deleteAt 的字段。我的聚合看起来像这样:

[
    { '$match': { _id: "5f0cc0e676de351ce21a752b" } },
    {
        '$lookup': {
            from: 'Exams',
            localField: 'exams.idExams',
            foreignField: '_id',
            as: 'exams'
        }
    },
    {
        '$lookup': {
            from: 'Sports',
            localField: 'idSports',
            foreignField: '_id',
            as: 'sportPracticed'
        }
    },
    {
        '$unwind': { path: '$sportPracticed', preserveNullAndEmptyArrays: true }
    },
    {
        '$lookup': {
            from: 'Galery',
            localField: '_id',
            foreignField: 'idPlayer',
            as: 'galery'
        }
    },
    {
        '$lookup': {
            from: 'EvaluationPlayer',
            localField: '_id',
            foreignField: 'idPlayer',
            as: 'evaluationPlayer'
        }
    },
    {
        '$lookup': {
            from: 'Evaluation',
            localField: 'evaluationPlayer.idEvaluation',
            foreignField: '_id',
            as: 'evaluations'
        }
    },
    {
        '$lookup': {
            from: 'Category',
            localField: 'evaluations.idCategory',
            foreignField: '_id',
            as: 'category'
        }
    },
    {
        '$lookup': {
            from: 'Club',
            localField: 'idClub',
            foreignField: '_id',
            as: 'club'
        }
    },
    { '$unwind': { path: '$club', preserveNullAndEmptyArrays: true } },
    {
        '$lookup': {
            from: 'Agent',
            localField: 'idAgent',
            foreignField: '_id',
            as: 'agent'
        }
    },
    { '$unwind': { path: '$agent', preserveNullAndEmptyArrays: true } },
    {
        $match: {
            $and: [
                { $exams: { $elemMatch: { deletedAt: { $exists: false } } } },
                { $sportPracticed: { deletedAt: { $exists: false } } },
                { $galery: { $elemMatch: { deletedAt: { $exists: false } } } },
                { $evaluationPlayer: { $elemMatch: { deletedAt: { $exists: false } } } },
                { $evaluations: { $elemMatch: { deletedAt: { $exists: false } } } },
                { $category: { $elemMatch: { deletedAt: { $exists: false } } } },
                { $club: { deletedAt: { $exists: false } } },
                { $agent: { deletedAt: { $exists: false } } },
            ]
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

但是,当执行此代码时,它返回错误:unknown top level operator: $ exams

我怎么解决这个问题 ?

我的计划不是从已经删除的关系中获取数据,并且说明它已被删除的是deleteadAt;

收藏玩家:

[{
  "_id": "5f0cc0e676de351ce21a752b",
  "language": "pt-br",
  "country": "BR",
  "status": true,
  "name": "Laura Silva",
  "nickname": "laurasilva",
  "dateOfBirth": "1995-05-01T00:00:00.000Z",
  "email": "laurasilva@gmail.com",
  "phones": [],
  "cpf": "54721452365",
  "gender": "F",
  "father": "Luiz Silva",
  "mother": "Larissa Silva",
  "weight": "67",
  "height": "1.67",
  "currentTeam": {
    "name": "América",
    "initialDate": "2020-01-01"
  },
  "professionalPlayer": true,
  "bird": "ARW1",
  "idSports": "5f0cbe10e6c0930b8dcc5181",
  "competitions": [],
  "exams": [],
  "preference": [],
  "coachReferrals": [],
  "createdAt": "2020-07-13T20:15:34.348Z",
  "updatedAt": "2020-07-13T20:15:34.348Z",
  "__v": 0,
  "idAgent": null,
  "idClub": null
},
{
  "_id": "5ee644c0280583764bfe7d97",
  "currentTeam": {
    "name": "Cruzeiro",
    "initialDate": "2019-02-25"
  },
  "network": {
    "instagram": {
      "url": "@renanmoaesoficial"
    }
  },
  "language": "pt-br",
  "country": "BR",
  "status": true,
  "name": "Renan Moraes",
  "nickname": "renanmoraes",
  "dateOfBirth": "1993-01-21T00:00:00.000Z",
  "email": "renan.desenvolviemnto@gmail.com",
  "phones": [
    {
      "_id": "5ee644c0280583764bfe7d98",
      "phone": "(31) 98796-1357"
    }
  ],
  "cpf": "123.367.952-85",
  "gender": "M",
  "father": "Antonio",
  "mother": "Sandra",
  "weight": "1,80",
  "height": "45",
  "professionalPlayer": false,
  "bird": "2551566655",
  "idSports": "5ee640d040b0a0649799c531",
  "exams": [
    {
      "results": [],
      "attestation": [],
      "_id": "5ee644c0280583764bfe7d9a",
      "idExams": "5ee6405f061a1362c3778435"
    },
    {
      "results": [],
      "attestation": [],
      "_id": "5ee644c0280583764bfe7d99",
      "idExams": "5ee6406e061a1362c3778436"
    }
  ],
  "standardPhoto": "https://exame.com/wp-content/uploads/2018/10/capaprofile.jpg?quality=70&strip=info",
  "competitions": [],
  "preference": [],
  "coachReferrals": [],
  "createdAt": "2020-06-14T15:39:44.198Z",
  "updatedAt": "2020-06-14T15:39:44.198Z",
  "__v": 0,
  "idAgent": null,
  "idClub": "5f176c8d58beb94efe56c59b"
}]
Run Code Online (Sandbox Code Playgroud)

集合体检:

{
  "_id": "5ee6406e061a1362c3778436",
  "status": true,
  "name": "Exames das prostatas",
  "value": "1",
  "type": "A",
  "createdAt": "2020-06-14T15:21:18.114Z",
  "updatedAt": "2020-06-14T15:21:18.114Z",
  "__v": 0
},
{
  "_id": "5ee6405f061a1362c3778435",
  "status": true,
  "name": "Exames dos rins",
  "value": "3",
  "type": "M",
  "createdAt": "2020-06-14T15:21:03.405Z",
  "updatedAt": "2020-06-14T15:21:03.405Z",
  "__v": 0
}
Run Code Online (Sandbox Code Playgroud)

在这个站点上有一个错误示例,在这种情况下,需要两个项目...... https://mongoplayground.net/p/IgnIt8wGvKq

hhh*_*a36 5

$match阶段中的关键参考不应该以$符号开头。

$$match舞台上移除标志。

此外,deletedAt最后两个条件的键不应放在单独的括号中。

{
        $match: {
            $and: [
                { exams: { $elemMatch: { deletedAt: { $exists: false } } } },
                { galery: { $elemMatch: { deletedAt: { $exists: false } } } },
                { evaluationPlayer: { $elemMatch: { deletedAt: { $exists: false } } } },
                { evaluations: { $elemMatch: { deletedAt: { $exists: false } } } },
                { category: { $elemMatch: { deletedAt: { $exists: false } } } },
                { "sportPracticed.deletedAt": { $exists: false } },
                { "club.deletedAt": { $exists: false } },
                { "agent.deletedAt": { $exists: false } },
            ]
        }
    }
Run Code Online (Sandbox Code Playgroud)