I have an collection “companies” and the documents have this structure:
id, name, address, branch, city
I want to add an keyword field that will have an index, so I can do a fulltext search, but how can I add a field to each document?
Thanks for help
There’s no schema in MongoDB, so you don’t have to add a field to every document.
Just start writing new documents with this field, or update old documents when you have this value for them.
As for indexing, now you can leverage sparse indexes, they will be more efficient if most of your documents don’t have this field.
Also, you might want this
keywordfield to be an array. It can be handled more efficiently than a string.If you want to add a field with the same value to all documents in a collection, you can use this:
When you do a
$set, you can’t use values from other fields. So if this new value is going to be a function of other fields, you have no other option, but to loop through the documents and update them one by one.