Reputation: 3434
I have created a Db and a collection in Mongodb. All the items in the collection have the following attributes: name, city, car and year
. I have around 200+ dummy items created in that collection. Now how do I go about adding 1 more attribute, like for example country
for each item?
Is this possible in Mongodb?
Upvotes: 3
Views: 10287
Reputation: 120
you can use db.getCollection('collectionName').update(findQuery, updateQuery, upsert, multi)
itself to insert a new column.
Example:
db.getCollection('test').update({}, {$set: {"country": "country"}},false,true)
This will insert a new column country with the default value country
Upvotes: 3
Reputation: 484
Are you using something that maps your application's objects to the database documents? If that's the case, you might have to add the new attribute there.
If you're just talking about plain mongodb, you can just add it to documents that have that attribute. Or iterate over all the documents and add it with some default value. You might actually have to do this as "kind of migration" to if your code requires this attribute on all of your documents.
Upvotes: 0