3

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?

2 Answers 2

3

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

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

Comments

0

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.

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.