admin管理员组

文章数量:1401184

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 plains: [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")]}}}])

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 plains: [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")]}}}])
Share Improve this question edited Oct 10, 2016 at 3:19 alexmngn asked Oct 7, 2016 at 6:06 alexmngnalexmngn 9,65720 gold badges75 silver badges136 bronze badges 7
  • You are missing "$oid" in the query. So try "user.$oid" instead of user in the used query. – Suresh Mahawar Commented Oct 7, 2016 at 6:31
  • Are you using some kind of custom _id fields, not Mongo's ObjectId instances? Usually you have to $nin: [ ObjectId(req.query.user.id) ] when querying against ObjectId-like fields. – Sergey Lapin Commented Oct 7, 2016 at 7:26
  • I use waterline (sailsjs/documentation/reference/waterline-orm) which automatically generate those id. – alexmngn Commented Oct 7, 2016 at 7:28
  • mongodb does not allow key name start with $ how you save the data with "$oid"? – Vaibhav Patil Commented Oct 7, 2016 at 9:55
  • 1 Have a look at this: stackoverflow./questions/38603529/… – 4J41 Commented Oct 10, 2016 at 4:25
 |  Show 2 more ments

1 Answer 1

Reset to default 1 +50

Based on the answer here, you can change

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

to

var ObjectId = require('sails-mongo/node_modules/mongodb').ObjectID;

本文标签: javascriptAggregate match pipeline not equal to in MongoDBStack Overflow