The $multiply function is only compatible with numerical data types, not arrays

Attempting to utilize the $multiply operator within MongoDB.

STEP 1

 db.message1064sd_00011_3744.aggregate([
    {$project : {
        "prices.00026" : 1,
         priceReal : {  
             price : "$prices.00026",
         }
    }},
    { $match : { "priceReal.price" : {$gt : 30 } } },
    { $limit : 1 }

])

Results obtained

 {
"result" : [ 
    {
        "_id" : "54884_00011_001",
        "prices" : {
            "00026" : 34.43
        },
        "priceReal" : {
            "price" : 34.43
        }
    }
],
"ok" : 1
}

STEP 2

However, upon using $multiply, the following error occurs:

$multiply only supports numeric types, not arrays

db.message1064sd_00011_3744.aggregate([

    {$project : {
        "prices.00026" : 1,
         priceReal : {  
             price : { $multiply : ["$prices.00026", 1 ] },
         }
    }},
    { $match : { "priceReal.price" : {$gt : 30 } } },
    { $limit : 1 }
])

Seeking assistance from anyone!

Sample document available via db.message1064sd_00011_3744.findOne()

{
"_id" : "25906_00011_001",
"Message_ID" : 25906,
"Subdivision_ID" : 3747,
"Sub_Class_ID" : 6300,
"Checked" : 1,
"Nomencl_ID" : "10000014597",
"manufacturer_ID" : "П1170",
"disableIfZero" : 0,
"Discontinued" : 0,
"New" : 0,
"Nomencl_Group_ID" : 28,
"Nalichie" : "Мало",
"sort" : 99,
"Warehouse_ID" : "00011",
"ParentWarehouse_ID" : "00011",
"Kachestvo" : "001",
"Svobod_Nalichie" : "10",
"Svobod_sort" : 10,
"character" : [],
"prices" : {
    "00014" : 1.51,
    "00015" : 1.45,
    "00016" : 1.41,
    "00017" : 1.38,
    "00018" : 1.35,
    "00019" : 1.33,
    "00021" : 1.31,
    "00022" : 1.29,
    "00023" : 1.28,
    "00024" : 1.27,
    "00025" : 1.25,
    "00026" : 1.24
},
"price" : {
    "Curr_ID" : 840,
    "ChangePriceTime" : "2017-01-22 19:18:21",
    "PriceUpDown" : "up",
    "callPrice" : 0,
    "Price_Value_RODP" : 1.24,
    "Price_Value_RUR" : 72.04000000000001
},
"sName" : "чип epson m2300{m2400{mx20 8k (elp, китай)",
"sNomencl_ID" : "10000014597",
"sNomencl_Articul_Proizvod" : "elp-ch-e2300-8k",
"sItemID" : "elp-ch-e2300-8k",
"EnglishName" : "cZ277",
"begin_vl" : 121,
"Hidden_URL" : "/netshop/cZ079/cZ270/cZ277/",
"Checked_Subdivision" : 1
}

Answer №1

To ensure that only data of a specific data type is included in your result set, such as in an aggregate query, you can incorporate the following filter into your matching criteria:

{$match: { 
     // ... insert your query here
     , "priceReal.price": { $type : "double" } 
} 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Reading data from a Node PassThrough stream after writing has finished can be achieved by piping the stream

Is it possible to write to a Node Passthrough stream and then read the data at a later time? I am encountering an issue where my code hangs when trying to do this. Below is a simplified example in Typescript: const stream = new PassThrough(); strea ...

Using an anonymous function in Javascript to change the background color of a div on mouse-over directly from the HTML code

I came across this code snippet in the provided link and as a beginner in JavaScript, I am unsure how to call the JavaScript function from the HTML code. Due to them being anonymous functions, I am struggling to invoke them. Can someone guide me on how to ...

Issue: NS_ERROR_FAILURE encountered in Firefox when using getBBox()

Is there a way to use the method getBBox() in SVG to retrieve the width and height of an element? I have included my code below, which works in Chrome but not in Firefox. If anyone has a solution to this issue, please let me know. try { console.log( ...

Building a Subscription Service with Express.js and MongoDB

Hello, I'm a beginner with express.js and I'm currently working on a project. The project involves a shop owner creating different plans with varying prices. My goal is to allow users to subscribe to a plan only after making a payment. Can someon ...

Guide on creating a darkening effect for lights

Seeking assistance on how to create a light-off effect when clicking on any of my textboxes. I discovered this concept on the following website: What I aim to achieve is that upon clicking a specific textbox, it will be highlighted. Upon clicking the sam ...

Using Three.js to create a React button positioned above a canvas

Here's the code I have written using three.js in a React component. I am looking to add a button above the canvas. How can I achieve this? Additionally, I would like to know how to add a click event on objects rendered in three.js. import React, { Com ...

Is there inconsistency in the behavior of json.parse when given the same input?

This query pertains to the differentiation in outputs resulting from various inputs I am not seeking guidance on achieving a specific output. The reason behind the discrepancy in output between two scenarios, despite using the same argument for the JS ...

Implementing the History API to dynamically load content into a div using Ajax

On my WordPress site, I have post content that loads into a div using AJAX when a user clicks on it. Now, I'd like to update the URL for the current post without using hashes. How can I achieve this with my JavaScript? JavaScript: jQuery(docum ...

How can I create a placeholder in semantic-ui components?

I'm currently utilizing the plugin and facing an issue with displaying the placeholder. Although data retrieval from the database is functioning properly, the placeholder is not being shown. Note:- When I remove the PHP code, the placeholder display ...

Struggling to find multiline content in a SWIFT message using regex

Looking into a SWIFT message using RegEx, here is an excerpt: :16R:FIN :35B:ISIN CH0117044708 ANTEILE -DT USD- SWISSCANTO (CH) INDEX EQUITY FUND USA :16R:FIA The goal is to extract information in group 3: ISIN CH0117044708 ANTEILE -DT USD- SWISSCANTO (C ...

Connect-Domain fails to detect errors in the scenario described below:

I have chosen to implement the connect-domain module (https://github.com/baryshev/connect-domain) in order to streamline error handling within my Express application. Although it generally functions as expected, there is a peculiar issue that arises when ...

Using jQuery's setInterval to consistently refresh the value of a data attribute

I am struggling to dynamically update the data-left value of a div with the class name "tw_marquee_scroller" every 1 second. The intended behavior is for the value to increment by 10 each time, starting at 10 and increasing by 10 in subsequent seconds. H ...

transferring information from php to json

Below is my JSON script: // add button .click $('a.add').click(function(){ $('#loader').show(); var url = "/?"+$("form[name='jsms_add']").serialize(); ajx = $.ajax({ url: url, type: 'post&a ...

Using Angular's NgFor directive to loop through a list of items and trigger a click event

Is it possible to programmatically trigger a click event on a specific item within an ngFor loop? <ul> <li class="list" *ngFor="let ver of versions; (click)="versionView()">{{ver.name}}</li> </ul> ...

Encountering a JavaScript toJSON custom method causing a StackOverflow error

Unique Scenario Upon discovering this answer, a new idea struck me - creating an object from a JSON literal. This led me to believe I could do the opposite using the handy JSON method: JSON.stringify(myObject). Curious, I proceeded as follows: function ...

Creating a dynamic Bootstrap 5 carousel with active class on the first item and indicators

I am curious to know how to create a dynamic Bootstrap 5 carousel. Specifically, I want to know how to display indicators dynamically and add an active class to the first item of the carousel. In Bootstrap 4, it is done in the following way: $('#main ...

Troubleshooting issue with error handling in graphql mutation hook with react and apollo is not resolving

It seems like I might have overlooked a configuration in my code, but I can't seem to pinpoint where I went wrong. In our application, we have a basic login form. If the correct username and password are entered, everything works smoothly. However, ...

Steps for eliminating the deprecation warning in a Node.js environment

Currently, I am utilizing the mongoose-ttl node package in order to manage collections. However, a warning message keeps appearing. (node:4768) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead. I have n ...

How can Angular be used to fetch the name associated with a user_id in Laravel?

There seems to be a problem with fetching the name attribute along with the title and body attributes. The name appears empty on page refresh but shows up automatically upon submission. Interestingly, angularjs is unable to retrieve the name successfully. ...

Why am I getting the "Cannot locate control by name" error in my Angular 9 application?

Currently, I am developing a "Tasks" application using Angular 9 and PHP. I encountered a Error: Cannot find control with name: <control name> issue while attempting to pre-fill the update form with existing data. Here is how the form is structured: ...