I am unable to utilize $cond for adding a new field to a MongoDB collection because the error states that multi update is not supported for replacement-style update

As I dive deeper into MongoDB commands, I encountered an issue that I'm struggling to resolve. Within my collection of many users, I am attempting to update all users by adding a new field. This field should be set to true if the user registered after May 5th, 2020, and false otherwise. Here is the code snippet I used:

db.users.updateMany({},
{
  $set: {
    "fidelity": {
      $cond: {
        if: {
          $gte: [
            "$registration",
            newDate("2020-05-05")
          ],
          then: true,
          else: false
        }
      }
    }
  }
}) 

The error message I received: https://i.sstatic.net/RID5K.png

Answer №1

When using $cond as an aggregation pipeline operator, make sure you are not mistakenly implementing a simple update instead. In this case, $cond may be misinterpreted as a nested key rather than an operator.

{$cond: {
        if: {
          $gte: [
            "$registration",
            newDate("2020-05-05")
          ],
          then: true,
          else: false
        }
      }}

This can lead to issues where the field name starting with $ is not recognized due to MongoDB limitations.

  • To utilize the aggregation pipeline in the update, ensure that the pipeline is provided as an array and refer to the documentation for accurate syntax.
  • If excluding $cond is necessary, explore alternative methods of expressing your intentions.

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

JavaScript: Converting an Array to a String

Having trouble making the string method work with an array - it keeps showing up as empty. let url = "https://api.myjson.com/bins/be7fc" let data =[]; fetch(url) .then(response => response.json()) .then(result => data.push(r ...

Halt period indicated in the document specifying the designated timeframe

If I have two files named index.php and fetch.php The contents of index.php are as follows: <script> $(document).ready(function(){ setInterval(function(){ $('#fetch').load('fetch.php') }, 1000); }); </sc ...

tips for decreasing box size diagonally while scrolling down

$(function(){ var navIsBig = true; var $nav = $('#header_nav'); $(document).scroll( function() { var value = $(this).scrollTop(); if ( value > 50 && navIsBig ){ $nav.animate({height:45},"medium"); $('.box ...

Importing CSV data into a Django database model on a routine basis

Currently, I am developing a web application that will be pulling data in CSV format from various sources on the internet. My plan is to aggregate this data using django and present it through a frontend for manipulation. I'm contemplating whether it ...

Alter the truth value of an item contained within an array

Embarking on my JavaScript journey, so please bear with me as I'm just getting started :) I am working on a small app where the images on the left side are stored in an array. When a user clicks on one of them, I want to change its height and also tog ...

What is the best way to eliminate a comma from a string if there is no value present?

When the properties are empty in the output, I notice a double comma (,,) in the middle and at the end of the string due to being separated by commas. How can I remove this so that there is only a single comma even when keys are empty? Expected Output: Au ...

How can we optimize the organization of nodes in a group?

While many questions focus on grouping nodes based on similarity, I am interested in grouping nodes simply based on their proximity. I have a vast collection of densely packed nodes, potentially numbering in the millions. These nodes take up space on-scre ...

What is the process for constructing an object to resemble another object?

After collecting input data, I have created an object based on those values. Here is an example of the generated object: var generate_fields = { name: "Mike", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4d9dddf ...

Error in delete operation due to CORS in Flask API

After successfully developing a rest api in Flask and testing all api endpoints with Postman, I encountered an issue while working on an application in Javascript that consumes the resources of my api. The problem lies in consuming an endpoint that uses t ...

Trouble with ES6 Arrow Functions, Syntax Error

I am encountering an issue with my JS class structure: class Tree { constructor(rootNode) { this._rootNode = rootNode; rootNode.makeRoot(); } getRoot() { return this._rootNode; } findNodeWithID(id) ...

Having trouble getting the HTML <a> href attribute to work in Javascript

I've been experimenting with implementing a live search feature using JavaScript in my Django project. The search by words is functioning properly, but I'm encountering an issue where only the titles are being displayed as results. My goal is to ...

Tips for assigning a Custom Formik Component's value to the Formik value

I'm currently integrating Formik into my form alongside Google Places auto-complete feature. My goal is to display the places auto-complete functionality as a custom component within the Formik field. form.js <Formik initialValues={location:" ...

Using Jquery to insert error messages that are returned by PHP using JSON

I am attempting to utilize AJAX to submit a form. I send the form to PHP which returns error messages in Json format. Everything works fine if there are no errors. However, if there are errors, I am unable to insert the error message. I am not sure why th ...

Combination of icons in JavaScript

Given a text array and an array of symbols, the current code performs the following: It looks for symbols in the text array and if the next element is also a symbol, it combines both symbols (the current one and the next one) with a '/' between ...

Issue with Heroku deployment: unable to locate JavaScript file

I encountered an issue while deploying my node.js app. Everything seemed to be working fine but it couldn't locate the JavaScript files. The error message displayed was: proove.herokuapp.com/:16 GET 404 (Not Found) Here is the server.js code snip ...

Category-specific WP_Query with Ajax Post Filter only triggers one time

I have successfully implemented an ajax post filter based on a helpful guide I found. The filter currently allows me to sort lectures by speaker and date, but I am now attempting to add a feature that filters lectures by category (which in this case is a s ...

Why does this particular check continue to generate an error, despite my prior validation to confirm its undefined status?

After making an AJAX call, I passed a collection of JSON objects. Among the datasets I received, some include the field C while others do not. Whenever I try to execute the following code snippet, it causes the system to crash. I attempted using both und ...

Why am I struggling to properly install React?

C\Windows\System32\cmd.e X + Microsoft Windows [Version 10.0.22621.2715] (c) Microsoft Corporation. All rights reserved. C:\Users\user\Desktop\React>npx create-react-app employee_app npm ERR! code ENOENT npm ERR! s ...

What is the best way to retrieve specific JSON data from an array in JavaScript using jQuery, especially when the property is

Forgive me if this question seems basic, I am new to learning javascript. I am currently working on a school project that involves using the holiday API. When querying with just the country and year, the JSON data I receive looks like the example below. ...

Learning to interpret JSON data in C# can be a valuable

I have developed a web service that accepts input in JSON format. Check out the code snippet below: Registration.asmx using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Script. ...