I\'m trying to negate an $and
clause with MongoDB and I\'m getting a MongoError: invalid operator: $and
message back. Basically what I want to achieve
I have a same problem, but not solved.
My collection have this itens (total 17):
{
"_id" : ObjectId("5d9d09dff9399554b35f4fc8"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
},
"createdAt" : ISODate("2019-10-08T22:12:47.872+0000"),
"updatedAt" : ISODate("2019-10-08T22:12:47.872+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9d09dff9399553df5f4fc9"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"createdAt" : ISODate("2019-10-08T22:12:47.883+0000"),
"updatedAt" : ISODate("2019-10-08T22:13:10.129+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9d0a00f93995da635f4fca"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "vacuo",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
},
"createdAt" : ISODate("2019-10-08T22:13:20.310+0000"),
"updatedAt" : ISODate("2019-10-08T22:13:20.310+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9d0a44de4e534ba8a1eaf6"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "nao-sugerir",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d833a2a7aebc4af5ba378ca")
},
"createdAt" : ISODate("2019-10-08T22:14:28.445+0000"),
"updatedAt" : ISODate("2019-10-08T22:14:28.445+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9d0a48de4e534ba8a1eaf7"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "nao-sugerir",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d82c2e07aebc4305da378b1")
},
"createdAt" : ISODate("2019-10-08T22:14:32.189+0000"),
"updatedAt" : ISODate("2019-10-08T22:14:32.189+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9d0b28de4e534ba8a1eaf8"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d82de827aebc42767a378c6")
},
"createdAt" : ISODate("2019-10-08T22:18:16.235+0000"),
"updatedAt" : ISODate("2019-10-08T22:18:16.235+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9d0b28de4e534ba8a1eaf9"),
"status" : {
"codigo" : "2",
"msg" : "Solicitação pendente"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d82de827aebc42767a378c6")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
},
"createdAt" : ISODate("2019-10-08T22:18:16.268+0000"),
"updatedAt" : ISODate("2019-10-08T22:18:16.268+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9d10eede4e534ba8a1eb0d"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d82755c7aebc49efca378aa")
},
"createdAt" : ISODate("2019-10-08T22:42:54.790+0000"),
"updatedAt" : ISODate("2019-10-08T22:42:54.790+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9d10eede4e534ba8a1eb0e"),
"status" : {
"codigo" : "2",
"msg" : "Solicitação pendente"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d82755c7aebc49efca378aa")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
},
"createdAt" : ISODate("2019-10-08T22:42:54.822+0000"),
"updatedAt" : ISODate("2019-10-08T22:42:54.822+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9dad94f939952d305f4fcc"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d82d8fa7aebc42be1a378c2")
},
"createdAt" : ISODate("2019-10-09T09:51:16.984+0000"),
"updatedAt" : ISODate("2019-10-09T09:51:16.984+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9dad94f939957a905f4fcd"),
"status" : {
"codigo" : "2",
"msg" : "Solicitação pendente"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d82d8fa7aebc42be1a378c2")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"createdAt" : ISODate("2019-10-09T09:51:16.998+0000"),
"updatedAt" : ISODate("2019-10-09T09:51:16.998+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9dad96f939956a345f4fce"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "vacuo",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d82d8fa7aebc42be1a378c2")
},
"createdAt" : ISODate("2019-10-09T09:51:18.587+0000"),
"updatedAt" : ISODate("2019-10-09T09:51:18.587+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9dce0cf939958be25f4fcf"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d8354497aebc439a7a378d0")
},
"createdAt" : ISODate("2019-10-09T12:09:48.619+0000"),
"updatedAt" : ISODate("2019-10-09T12:09:48.619+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9dce0cf939958c4b5f4fd0"),
"status" : {
"codigo" : "2",
"msg" : "Solicitação pendente"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d8354497aebc439a7a378d0")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"createdAt" : ISODate("2019-10-09T12:09:48.635+0000"),
"updatedAt" : ISODate("2019-10-09T12:09:48.635+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9dce10f9399590a65f4fd1"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d685f790215c983e4fd097e")
},
"createdAt" : ISODate("2019-10-09T12:09:52.994+0000"),
"updatedAt" : ISODate("2019-10-09T12:09:52.994+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9dce11f9399564715f4fd2"),
"status" : {
"codigo" : "2",
"msg" : "Solicitação pendente"
},
"extra" : [
],
"tipo" : "amizade",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d685f790215c983e4fd097e")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"createdAt" : ISODate("2019-10-09T12:09:53.006+0000"),
"updatedAt" : ISODate("2019-10-09T12:09:53.006+0000"),
"__v" : NumberInt(0)
}
// ----------------------------------------------
{
"_id" : ObjectId("5d9e4afbde4e534ba8a1eb0f"),
"status" : {
"codigo" : "1",
"msg" : "Ativo"
},
"extra" : [
],
"tipo" : "nao-sugerir",
"vinculo" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d69a8eb0215c92942fd0983")
},
"vinculado" : {
"colecao" : "atletas",
"objeto" : ObjectId("5d80f31e7aebc412dea3789a")
},
"createdAt" : ISODate("2019-10-09T21:02:51.026+0000"),
"updatedAt" : ISODate("2019-10-09T21:02:51.026+0000"),
"__v" : NumberInt(0)
}
In this query, returns correct (4 itens):
db.getCollection("vinculos").aggregate(
[
{
"$match" : {
"status.codigo" : "1",
"tipo" : {
"$in" : [
"amizade",
"nao-sugerir"
]
},
"vinculo.objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
}
}
]
)
But i need a inverse of this itens:
1st attempt (No Results):
db.getCollection("vinculos").aggregate(
[
{
"$match" : {
"$nor" : [
{
"status.codigo" : "1"
},
{
"tipo" : {
"$in" : [
"amizade",
"nao-sugerir"
]
}
},
{
"vinculo.objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
}
]
}
}
]
)
2nd attempt (No Results):
db.getCollection("vinculos").aggregate(
[
{
"$match" : {
"$and" : [
{
"status.codigo" : {
"$ne" : "1"
}
},
{
"tipo" : {
"$nin" : [
"amizade",
"nao-sugerir"
]
}
},
{
"vinculo.objeto" : {
"$ne" : ObjectId("5d6821c70215c9e5e5fd096b")
}
}
]
}
}
]
)
I will need return other 13 itens, any suggestion?