Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MongoDB support XOR (exclusive OR)?

Tags:

mongodb

I am looking for a way to do exclusive OR on MongoDB.

For instance, $or works as expected:

> db.mycollection.find({ '$or': [ { 'a': 1 }, { 'b': 1 } ] })

But I would need to find records where a is 1 or b is 1, but not both of them. Something like this:

> db.mycollection.find({ '$xor': [ { 'a': 1 }, { 'b': 1 } ] })

(doesn't work - fictional syntax)

Does MongoDB support XOR logical operator? If not, how can it best be simulated?

like image 625
johndodo Avatar asked Oct 08 '14 09:10

johndodo


People also ask

Does MongoDB support locking functionalities?

MongoDB uses multi-granularity locking [1] that allows operations to lock at the global, database or collection level, and allows for individual storage engines to implement their own concurrency control below the collection level (e.g., at the document-level in WiredTiger).

How $or works in MongoDB?

MongoDB provides different types of logical query operators and $or operator is one of them. This operator is used to perform logical OR operation on the array of two or more expressions and select or retrieve only those documents that match at least one of the given expression in the array.

How do I lock a document in MongoDB?

In MongoDB we recommend using the findAndModify command for this scenario. This command is atomic and thus lock the document for a status change. Each service instance should do: db.

Which lock in MongoDB provides Kaun currency?

MongoDB uses reader-writer locks that allow concurrent readers shared access to a resource, such as a database or collection, but in MMAPv1, give exclusive access to a single write operation.


1 Answers

Does this satisfy your expectation?

db.mycollection.find({ '$or': [ { 'a': 1 , 'b' : {$ne : 1} }, { 'b': 1, 'a' : {$ne : 1} } ] })
like image 94
Wizard Avatar answered Sep 21 '22 14:09

Wizard