MongoDB未知顶级运算符
我对mongo的加积门有问题,我需要验证所有答案中是否没有标记为deletedAt的字段。我的大门是这样的:MongoDB未知顶级运算符,mongodb,mongodb-query,Mongodb,Mongodb Query,我对mongo的加积门有问题,我需要验证所有答案中是否没有标记为deletedAt的字段。我的大门是这样的: [ { '$match': { _id: "5f0cc0e676de351ce21a752b" } }, { '$lookup': { from: 'Exams', localField: 'exams.idExams', foreignField: '_id',
[
{ '$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 } } },
]
}
}
]
但是,当执行此代码时,它返回错误:未知顶级运算符:$EXCESS
我怎样才能解决这个问题
我的计划不是从已经被删除的关系中获取数据,而说它被删除的是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"
}]
收集医疗辅助器材:
{
"_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
}
在这个网站上有一个错误的例子,在这种情况下,这两个项目将需要来 $match阶段中的关键引用不应以$sign开头 从$match阶段删除$sign 另外,最后两个条件的deletedAtkey不应在单独的括号中
{
$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 } },
]
}
}
嗯,我设法解决了我的问题。。事实证明,我需要为每个现有的查找关系创建一个管道,注意以正确的方式查找数据 我有在数组中建立的关系,我需要将数组中的每个项目与另一个集合中的id相匹配。为了实现这一点,我添加了let来声明要在查找中使用的变量,然后使用$in对这些ID进行特定搜索 不管怎么说,最终的目标是这样的,我采取了一些项目,也不重要,使它更简单
{
"$lookup": {
"from": "Exams",
"let": {
"exams_items": "$exams.idExams"
},
"pipeline": [
{
"$match": {
"$expr": {
"$in": [
"$_id",
"$$exams_items"
]
},
"deletedAt": {
$exists: false
}
}
}
],
"as": "allExams"
}
},
{
"$lookup": {
"from": "Sports",
"let": {
"id_sport": "$idSports"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$_id",
"$$id_sport"
]
},
"deletedAt": {
$exists: false
}
}
}
],
"as": "sportPracticed"
}
},
{
$unwind: {
"path": "$sportPracticed",
"preserveNullAndEmptyArrays": true
}
},
{
"$lookup": {
"from": "Galery",
"let": {
"id_player": "$_id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$idPlayer",
"$$id_player"
]
},
"deletedAt": {
$exists: false
}
}
}
],
"as": "galery"
}
},
{
"$lookup": {
"from": "EvaluationPlayer",
"let": {
"id_player": "$_id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$idPlayer",
"$$id_player"
]
},
"deletedAt": {
$exists: false
}
}
}
],
"as": "evaluationPlayer"
}
},
{
$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
}
}
不要在$EXAMES和其他字段中使用$EXAMES。。。当你在比赛时。检查这里,@viagem可能问题是当您使用其他matchstry来模拟所有关系时,甚至返回空。因为在我的例子中,它不工作时,它有不返回值的关系。。例如,我有画廊,如果没有项目,它必须返回一个空数组。。。这不是这样做的,你知道它可以是什么吗?好的,等待片刻我更新了问题,添加了一个基本示例。如果我做了一个findAll,它应该返回所有包含关系的我。在这个网站上有一个错误的例子,在这种情况下,这两个项目将需要来。。。很高兴你把它修好了;