MongoDB 使用 NOT of AND 列表文档

MongoDB using NOT of AND list documents

我有关于取回不在条件列表中的文件的问题。

我尝试了几种方法,但 none return 编辑了他们应该 return.

的 13 个项目

我的 collection 有这个项目(总共 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)
}

在这个查询中,returns 正确(4 itens):

db.getCollection("vinculos").aggregate(
    [
        { 
            "$match" : {
                "status.codigo" : "1", 
                "tipo" : {
                    "$in" : [
                        "amizade", 
                        "nao-sugerir"
                    ]
                }, 
                "vinculo.objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
            }
        }
    ]
)

但我需要这个 itens 的倒数:

第一次尝试(无结果)

db.getCollection("vinculos").aggregate(
    [
        { 
            "$match" : {
                "$nor" : [
                    {
                        "status.codigo" : "1"
                    }, 
                    {
                        "tipo" : {
                            "$in" : [
                                "amizade", 
                                "nao-sugerir"
                            ]
                        }
                    }, 
                    {
                        "vinculo.objeto" : ObjectId("5d6821c70215c9e5e5fd096b")
                    }
                ]
            }
        }
    ]
)

第二次尝试(无结果)

db.getCollection("vinculos").aggregate(
    [
        { 
            "$match" : {
                "$and" : [
                    {
                        "status.codigo" : {
                            "$ne" : "1"
                        }
                    }, 
                    {
                        "tipo" : {
                            "$nin" : [
                                "amizade", 
                                "nao-sugerir"
                            ]
                        }
                    }, 
                    {
                        "vinculo.objeto" : {
                            "$ne" : ObjectId("5d6821c70215c9e5e5fd096b")
                        }
                    }
                ]
            }
        }
    ]
)

我需要 return 其他 13 个项目,有什么建议吗?

为了补充过滤器,我们可以使用 De Morgan's law

Not ( A and B and C) = Not(A) or Not(B) or Not(C)

以下查询可以获得预期的输出:

db.getCollection("vinculos").aggregate(
    [
        { 
            "$match" : {
                $or:[
                    {
                        "status.codigo" :{
                            $ne: "1"
                        } 
                    }, 
                    {
                        "tipo" : {
                            "$nin" : [
                                "amizade", 
                                "nao-sugerir"
                            ]
                        }
                    }, 
                    {
                        "vinculo.objeto" : {
                            $ne: ObjectId("5d6821c70215c9e5e5fd096b")
                        }
                    }
                ]
            }
        }
    ]
)