Adding a key/value pair to an array item in pymongo

Viewed 69

I have a MongoDB object that looks like this:

_id: ObjectID
remote_address: [
0: Foo
1: Bar
]

I use collection.update_one({data}, {"$push": {data}}) to add items to the array.

However, is it possible to make an array item another object? Kind of like so:

_id: ObjectID
remote_address: [
0: Foo {
  Foo: Bar
}
1: Bar {
  Foo: Bar
}
]

Thanks!

1 Answers

You can add objects to arrays quite happily, e.g.:

db.mycollection.insert_one({'remote_address': [{'Foo': {'Foo': 'Bar'}}, {'Bar': {'Foo': 'Bar'}}]})
Related