Linchakin

26th Sep - MongoDB $cmp Operator

 September 26, 2021     No comments   

What is the $cmp operator in MongoDB?

MongoDB provides a variety of aggregation pipeline operators. The $cmp operator is one of those operators. The $cmp operator is used to compare two values or types and returns the result.

It returns the following result according to these conditions:

  • If both values are equal, it returns 0.
  • If the first value is greater than the second value, it returns 1.
  • If the first value is less than the second value, it returns -1.

Syntax of the $log10 operator:

Examples:

Suppose we have a collection of the products with the following documents.

Example 1: Using $cmp Operator

In this example, we're going to compare the length and width fields using the $cmp operator.

Output:

{
"_id" : 1,
"name" : "rectangle",
"length" : 11,
"breath" : 10,
"result" : 1
}
{
"_id" : 2,
"name" : "square",
"length" : 10,
"breath" : 10,
"result" : 0
}
{
"_id" : 3,
"name" : "rectangle",
"length" : 14,
"breath" : 16,
"result" : -1
}
{
"_id" : 4,
"name" : "square",
"length" : 6,
"breath" : 6,
"result" : 0
}
{
"_id" : 5,
"name" : "rectangle",
"length" : 11,
"breath" : 16,
"result" : -1
}

Suppose we add the following documents to our collection:

Example 2: Comparing null values

Output:

{
"_id" : 6,
"name" : "rectangle",
"length" : null,
"breath" : 10,
"result" : -1
}
{
"_id" : 7,
"name" : "square",
"length" : 10,
"breath" : null,
"result" : 1
}
{
"_id" : 8,
"name" : "rectangle",
"length" : null,
"breath" : null,
"result" : 0
}

Suppose we add the following documents to our collection:

Example 3: Comparing different types

Output:

{
"_id" : 9,
"name" : "item_1",
"a" : ISODate("2020-05-09T23:30:15.100Z"),
"b" : 10,
"result" : 1
}
{
"_id" : 10,
"name" : "item_2",
"a" : 10,
"b" : ISODate("2012-11-18T04:00:20.112Z"),
"result" : -1
}
{
"_id" : 11,
"name" : "item_3",
"a" : ISODate("2018-11-14T04:00:20.112Z"),
"b" : ISODate("2015-10-07T04:00:20.112Z"),
"result" : 1
}

Next Topic#

Adblock test (Why?)


You may be interested in:
>> Is a Chromebook worth replacing a Windows laptop?
>> Find out in detail the outstanding features of Google Pixel 4a
>> Top 7 best earbuds you should not miss

Related Posts:
>> Recognizing 12 Basic Body Shapes To Choose Better Clothes
>>Ranking the 10 most used smart technology devices
>> Top 5+ Best E-readers: Compact & Convenient Pen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • AOC's 43-Inch G4309VX/D Display Boasts 144Hz 4K VA Panel, HDMI 2.1ByBrandon Hill published 6 December 21The AOC G4309VX/D packs quite a punch in the 4...Another large-format gaming monitor is on the way from AOC (via TFT Central), and it looks promising. The company just announced the G4309VX/D, which … Read More
  • Microsoft’s Loop Project Management Software Microsoft has been developing a project collaboration solution for enterprise projects. The Loop app is now accessible to Microsoft 365 commercial cu… Read More
  • Poll Data: What CEOs Need to Know About Cybersecurity Going into 2022The end of the year is a good time to reflect on the past 12 months and create a plan to improve in 2022. Like years past, 2021 revealed more of the s… Read More
  • Strap in Apple fans, this might be the wildest Mac rumour yetByAmelia Bamsey published 6 December 21NewsIf this is true, Apple is going to be very bus...It's been just over a month since Apple announced the release of its brand new MacBook Pro with its controversial design and fateful new notch. But th… Read More
  • AMD FX-6300 Hits 8 GHz, Scoring a Minor Overclocking RecordByAaron Klotz published 6 December 21An FX-6300 just broke an overclocking record by reachi... After a long hiatus in the overclocking world, AMD's FX processors are back to breaking world records in 2021--just not with a Ryzen chip. Tweeted by… Read More
Newer Post Older Post Home

0 Comments:

Post a Comment


Copyright © 2025 Linchakin | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates