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?

Laughing_Man
  • 179
  • 16

1 Answers1

0

You can do like this, without $getField:

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

Here's the working link.

Charchit Kapoor
  • 8,934
  • 2
  • 8
  • 24