Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate match pipeline not equal to in MongoDB

I am working on an aggregate pipeline for MongoDB, and I am trying to retrieve items where the user is not equal to a variable.

For some reason, I couldn't make it work. I tried to use $not, $ne and $nin in different possible way but can't make it to work.

This is how it looks like:

Data sample:

[{
    "_id": { "$oid": "565674e2e4b030fba33d8fdc" },
    "user": { "$oid": "565674832b85ce78732b7529" }
}, {
    "_id": { "$oid": "565674e2e4b030fba33d8fdc" },
    "user": { "$oid": "565674832b85ce78732b7529" }
}, {
    "_id": { "$oid": "565674e2e4b030fba33d8fdc" },
    "user": { "$oid": "56f9dfc5cc03ec883f7675d0" }
}]

Pipeline sample (simplified for this question):

Where req.query.user.id = "565674832b85ce78732b7529"

collection.aggregate([
    {
        $match: {
            user: {
                $nin: [ req.query.user.id ],
            }
        }
    }
]

This should return only the last item.

Do you have any idea how to retrieve the data that doesn't match the user?

Thanks

Edit: The following doesn't work either:

collection.aggregate([
    {
        $match: {
            'user.$oid': {
                $nin: [ req.query.user.id ],
            }
        }
    }
]);

I also tried with ObjectID() and mongodb complains: [MongoError: Argument must be a string]

var ObjectID = require('mongodb').ObjectID;

// Waterline syntax here
MyCollection.native(function (err, collection) {
    collection.aggregate([
        {
            $match: {
                'user': {
                    $nin: [ ObjectID(req.query.user.id) ],
                }
            }
        }
    ], function (err, result) {
        console.log(err, result);
    });
});

But this line works in the shell:

db.collection.aggregate([{$match:{"user":{"$nin":[ObjectId("565674832b85ce78732b7529")]}}}])
like image 210
alexmngn Avatar asked Oct 07 '16 06:10

alexmngn


People also ask

Is not equal to MongoDB?

MongoDB provides different types of comparison operators and inequality or not equals operator( $ne ) is one of them. This operator is used to select those documents where the value of the field does not equal to the given value. It also includes those documents that do not contain the specified field.

What passes through a MongoDB aggregation pipeline?

An aggregation pipeline consists of one or more stages that process documents: Each stage performs an operation on the input documents. For example, a stage can filter documents, group documents, and calculate values. The documents that are output from a stage are passed to the next stage.

How do I write not in condition in MongoDB?

$not performs a logical NOT operation on the specified <operator-expression> and selects the documents that do not match the <operator-expression> . This includes documents that do not contain the field .

Which is not aggregation in MongoDB?

In MongoDB, the $not aggregation pipeline operator evaluates a boolean and returns the opposite boolean value. In other words, when the boolean evaluates to true , the $not operator returns false . And when the boolean evaluates to false , the $not operator returns true .


1 Answers

Based on the answer here, you can change

var ObjectId = require('mongodb'). ObjectID;

to

var ObjectId = require('sails-mongo/node_modules/mongodb').ObjectID;
like image 183
4J41 Avatar answered Oct 04 '22 03:10

4J41