MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

How to use $nor operator in Compass Aggregation Pipeline

Hi everyone

I was wondering how to implement the $nor operator in MongoDB Compass aggregation pipeline.

Can anyone help me with this issue please?

Sincerely
Ezequias Rocha

Hi @Ezequias_Rocha!

I don’t know if I understood correctly, but follow an example using a pipeline where $nor is applied to the $match stage. Consider the following documents in the sales collection:

{
	"_id" : 1,
	"item" : "abc",
	"price" : 10,
	"fee" : 2,
	"date" : ISODate("2014-03-01T08:00:00Z")
}
{
	"_id" : 2,
	"item" : "jkl",
	"price" : 20,
	"fee" : 1,
	"date" : ISODate("2014-03-01T09:00:00Z")
}
{
	"_id" : 3,
	"item" : "xyz",
	"price" : 5,
	"fee" : 0,
	"date" : ISODate("2014-03-15T09:00:00Z")
}

To search only documents where the price field is not equal to 20 or the fee field is not equal to 2, it would be as follows:

db.sales.aggregate(
    [
        {
            $match: {
                $nor: [
                    { price: 20 },
                    { fee: 2 }
                ]
            }
        }
    ]
)

I hope I have helped, otherwise detail your need a little more!

Hi @Leandro_Domingues

It could work but it is not working in MongoDB Compass. Could you check this out at this tool?

Regards
Ezequias.

The aggregation by @Leandro_Domingues works fine in the mongo shell. But, somehow it doesn’t in the MongoDB Compass (using version 1.19.6). I have the MongoDB version 4.2.3.

The way the $match stage filter is specified in the Compass GUI is:

/**
 * query - The query in MQL.
 */
{
  $nor: [ { "price": 20 }, { "fee": 2 } ]
}

There is an error: Expected "[" or AggregationStage but "{" found.

The aggregation query with $or works fine in the Compass, e.g.,:

{
  $or: [ { "price": 20 }, { "fee": 2 } ]
}
1 Like

Right @Prasad_Saya

Could someone open an issue in MongoDB Jira please?

It’s strange noone have already tested if before.

Best wishes
Ezequias Rocha

The issue: https://jira.mongodb.org/browse/COMPASS-4241

1 Like

Hi Ezequias,

FYI, you can also create bug reports in Jira. There’s single sign-on with the same account you are using for the community forum: https://jira.mongodb.org/browse/COMPASS.

Regards,
Stennie

This is fixed in the upcoming 1.21 release.

1 Like