簡體   English   中英

聚合匹配管道不等於MongoDB

[英]Aggregate match pipeline not equal to in MongoDB

我正在為MongoDB的聚合管道工作,我正在嘗試檢索用戶不等於變量的項目。

出於某種原因,我無法使其發揮作用。 我嘗試以不同的方式使用$not$ne$nin ,但無法使其工作。

這是它的樣子:

數據樣本:

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

管道樣本(針對此問題進行了簡化):

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

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

這應該只返回最后一項。

您是否知道如何檢索與用戶不匹配的數據?

謝謝

編輯:以下不起作用:

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

我也試過ObjectID()和mongodb抱怨: [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);
    });
});

但這行在shell中有效:

db.collection.aggregate([{$match:{"user":{"$nin":[ObjectId("565674832b85ce78732b7529")]}}}])

根據這里的答案,你可以改變

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM