简体   繁体   中英

MongoDB using NOT and AND together

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 is the following:

query = { $not: { $and: [{institution_type:'A'}, {type:'C'}] } }

Is this possible to express in a mongo query?

Here is a sample collection:

{ "institution_type" : "A", "type" : "C" }
{ "institution_type" : "A", "type" : "D" }
{ "institution_type" : "B", "type" : "C" }
{ "institution_type" : "B", "type" : "D" }

What I want to get back is the following:

{ "institution_type" : "A", "type" : "D" }
{ "institution_type" : "B", "type" : "C" }
{ "institution_type" : "B", "type" : "D" }

You're looking for NOT (A AND C) , which is equivalent to NOT A OR NOT C :

db.collection.find({
  "$or": [
    {"institution_type": {"$ne": "A"}},
    {"type": {"$ne": "C"}}
  ]
})

MongoDB also has a $nor logical operator that "performs a logical NOR operation on an array of one or more query expression and selects the documents that fail all the query expressions in the array", so an equivalent query would be:

db.collection.find({
  "$nor": [
    {"institution_type": "A"},
    {"type": "C"}
  ]
})

The accepted answer recommends using a $where operator, but that is unnecessary here and taxes performance.

Not a standard negation that I can see, you would need a $where operator styled query with JavaScript:

db.collection.find(function(){ 
    return !( this.institution_type == "A" && this.type == "C" ) 
})

Not the best as that scans a whole collection, but the negation in your logic requires this as you need to test both values.

Or the essentially equivalent aggregation operation:

db.collection.aggregate([
    { "$project": {
        "institution_type": 1,
        "type": 1,
        "notmatched": {
            "$not": {
                "$and": [
                    { "eq": [ "$institution_type", "A" ] },
                    { "eq": [ "$type", "C" ] }
                ]
            }
        }
    }},
    { "$match": { 
        "notmatched": true
    }}
])

Only just realized this was also a valid form by including the negation on the inside:

db.collection.find({
    "$or": [
        { "institution_type": { "$ne": "A" } }, 
        { "type": { "$ne": "C" }},
    ]
})

Where that essentially negates the combination of "A" and "C" in the same document just as the other logic does with a wrapping "not" condition.


Orig

In MongoDB queries the $and is actually implicit since that is the default comparison of terms in a query. You only need to use $and when you are looking for multiple conditions on the same field.

But your query is a simple inequality match:

db.collection.find({
    "institution_type": { "$ne": "bank" },
    "type": { "$ne": "account_type" }
})

So you use the $ne operator to negate your terms from a match, where as already stated the $and is implicit so both conditions must apply.

So with sample data:

{ "institution_type" : "bank", "type" : "account_type" }
{ "institution_type" : "school", "type" : "account_type" }
{ "institution_type" : "school", "type" : "account" }

The query only returns the row that does not meet both conditions:

{ "institution_type" : "school", "type" : "account" }

Your comment says you wan "OR" which you do not specify in your query as you are using "AND", so perhaps you are expecting that "OR" is implicit which is the opposite of what I have told you is the case.

An $or condition needs to be explicitly applied:

db.collection.find({
    "$or": [
        { "inititution_type": { "$ne": "bank" } },
        { "type": { "$ne": "account_type" } }
    ]
})

Which is actually double negation and would return all results.

Perhaps you mean $nor :

db.data.find({
    "$nor": [
        { "inititution_type": "bank" },
        { "type": "account_type"  }
    ]
})

But actually that is logically the same as the first query I gave you.

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?

TLDR; You want a NAND Gate...

db.getCollection('test').find({ $or: [ {institution_type:{$ne:'A'}}, {type:{$ne:'C'}} ]})

... And then go thank (upvote) this answer (not me)

or a NOR Gate...

db.getCollection('test').find({ $nor: [{institution_type:'A', type:'C'}] })

Longer version...

I couldn't find a logical operator in MongoDB to handle $nand logic, so I "built" the logic (see below).

There is an open feature request for a $nand operator, but I wouldn't hold my breath (created 2014).

$nor can also do what you want, if you invert the $ne logic.

(But IMO - it feels like we're bastardising the behaviour - only using the inverse $not behaviour, and disregarding the $and behaviour of the array... but it works, so it's not wrong).

db.getCollection('test').insertMany([
    { "institution_type" : "A", "type" : "C" },
    { "institution_type" : "A", "type" : "D" },
    { "institution_type" : "B", "type" : "C" },
    { "institution_type" : "B", "type" : "D" }
])

// "NAND gate": (NOT A) OR (NOT B) -> correct 3x results
db.getCollection('test').find({ $or: [ {institution_type:{$not:{$eq:'A'}}}, {type:{$not:{$eq:'C'}}} ]})

// or simpler...
db.getCollection('test').find({ $or: [ {institution_type:{$ne:'A'}}, {type:{$ne:'C'}} ]})

// or simpler still...

// "NOR gate": (NOT A) AND (NOT B) -> correct 3x results
db.getCollection('test').find({ $nor: [{institution_type:'A', type:'C'}] })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM