2

I have a this document.Which has a schema of below:

 rental:{
    total:Number,
    due:Number
}

For example let us assume the document is filled with values like this:

 rental:{
    total:350,
    due:10
}

I want to replace the value of 'total' to 'due'.So i want it to be like this:

 rental:{
    total:350,
    due:350
}

I came accross $set,i did something like this: PS:"User" is the name of the model.(which i havent refrenced here)

User.updateMany({},{$set:{'due':"$total"}},function(err,..}{ 
//do whatever
    }

But this didnt work out.I ran into a CastError. I also came accross '$replaceWith'.But i didnt understand a bit on how to use that in my case.Any help is appriciated.Thank you

1 Answer 1

1

You can use below query

db.collection.update(
  { },
  [{ "$set": { "due": "$total" }}]
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.