Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
copying values inside a record from one key to other in mongodb

I am working on MongoDB. I am trying to append some new key-value pairs to my existing collection. I got more than 50 documents in my collection, in all of which I want to update a new key value pair. In the new key value pair I am adding, one of the values of the key should be from one of the existing key value pair of the same collection. For example:

{ name : "algebra", quantity : 25 }

Now I want to update each of the document with

{ category : "maths" , available_quantity : 25 }

Without entering distinctly for every document its available quantity. I want it to be picked from it's respective quantity's value, and be inserted in the new key available_quantity. I am using the code to update the document :

db.books.update( {} , { $set : { category : "maths" , available_quantity : `__ ? __` }, false, true }

What best can I put in __ ? __ block?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can do it with Aggregation framework:

db.books.update({},
[
  {
    "$set": {
      "available_quantity": "$quantity"
    }
  }
],
{
  multi: true
})

Working example

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!