0

I have a simple mongoDB document:

{
  _id: ObjectId("5c8eccc1caa187d17ca6ed16"),
  city: 'ALPINE',
  zip: '35014',
  loc: { y: 33.331165, x: 86.208934 },
  pop: 3062,
  state: 'AL'
}

The only thing I want to do is to add a new property population and set it with a value from pop property.

I've tried to use $getField like this: updateOne({}, {$set: {"population": { $getField: "pop" }}}). But the result is too straightforward xD

{
  _id: ObjectId("5c8eccc1caa187d17ca6ed16"),
  city: 'ALPINE',
  zip: '35014',
  loc: { y: 33.331165, x: 86.208934 },
  pop: 3062,
  state: 'AL',
  population: { '$getField': 'pop' }
}

So how can I set population field to value from pop field?

0

1 Answer 1

0

You can do like this, without $getField:

db.collection.update({},
[
  {
    $set: {
      "population": "$pop"
    }
  }
])

Here's the working link.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.