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 keyword field 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:
db.collection.update({}, // update all documents
{$set : {keyword : []}}, // or another value
false, // is upsert?
true) // is multi-update?
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.
Related
I'm have a huge documents in a collection. I want to remove auto generated Object Id (_id key) from all the documents and replace it with another field as a Primary key?
I don't understand is why is there a need for a default Object Id in first place?
In mongodb each document must be unique, so you need an unique field to be used as id. If you do not provide one, mongodb will provide one for you automatically. However, if you want to give custom ids for whichever reason (improve query performance being one of them), you can do it manually. Here goes my suggestions:
If you are inserting a new document, you can manually set the _id field like:
doc._id = "12312312" //(Or whichever function to generate id you have)
doc.save(...)
But when you already have a document in the database, you cannot modify it anymore. What you can do is to make a copy of the document, save a new document with the same data and erase the old one:
// Fetch the documents
docs = db.clients.find({})
docs.forEach((doc) => {
//For each field you copy the values
new_doc.field = doc.field
new_doc._id = //YOUR ID FUNCTION
// insert the document, using the new _id
db.clients.insert(new_doc)
// remove the document with the old _id
db.clients.remove({_id: doc._id})
}
This question is similar to the following one:
How update the _id of one MongoDB Document?
Hope my answer was helpful
It is not possible to remove the _id field.
From https://docs.mongodb.com/manual/core/document/#the-id-field:
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.
What you can do is name your primary key as _id.
Lets say you have a collection with a field called "primary_key",
{"primary_key":"1234", "name":"jimmy", "lastname":"page"}
and I have an index on "primary_key".
This collection has millions of rows, I want to see how expensive is to change primary_key for one of the records. Does it trigger a reindex of the entire table? or does it just reindex the changed record? in either case is that expensive to do?
Updating an indexed field in mongodb causes an update of the index (or indices if you have more than one) that use it. It does not "reindex". Shouldn't be all that expensive - effectively you will delete the old entry and insert a new one.
This document has a fair amount of detail on mongodb indexes:
http://docs.mongodb.org/master/MongoDB-indexes-guide.pdf
BTW, keep in mind that there is one special field, _id, that mongodb uses as it's primary key
_id
A field required in every MongoDB document. The _id field must have a unique value. You can think of the _id field as the document’s
primary key. If you create a new document without an _id field,
MongoDB automatically creates the field and assigns a unique BSON
ObjectId.
You cannot update the _id field.
I am experimenting with creating a text index in MongoDB for several fields in a sub-document. These fields are not the same from document to document. According to the documentation, I would create a normal text index like so:
db.collection.ensureIndex({
subject: "text",
content: "text"
});
In my case, I want the index on all fields in the fs.files collection at db.fs.files.metadata. I've tried this:
db.fs.files.ensureIndex({'metadata.$**': 'text'});
I don't believe this has worked, as searching with db.fs.files.runCommand('text'... returns zero results, and db.fs.files.stats() shows the index as a very small size (and I have ~35k documents in this collection).
How can I create a text index on field values of a subdocument where the keys are not known ahead of time?
If you create a {'$**': 'text'} index on the parent document it will index the subdocument fields too. The docs say it only affects text so it will skip the file data but will include the name & contentType.
MongoDB can store documents with different fields in one collection.
How then indexes will work? If I create index on field that presents not in all documents, the documents which don't have that will not be indexed?
Documents without the field in an index will be indexed as having no value for that field. You probably want to review this: http://docs.mongodb.org/manual/core/indexes/
If you want to not include documents that don't have the key in the index, you can use a sparse index: http://docs.mongodb.org/manual/administration/indexes/#sparse-indexes
I currently have a collection of small documents. Each document has an indexed geospacial field and *the default _id is never used in any query*. There will never be more than one document related to a particular geo location. I think it makes sense to override the default _id, and use the geospacial data for this somehow.
Question is, how do you use geospacial data as the unique id? Is it a case of creating a flat string from the geo field? E.g. 'x123456y123456'?
The _id field is the unique identifier for each document and thus is a needed field. The _id field is generated on document creation automatically if one is not provided. If you can provide this geospaital value when creating the document you should be able to use the string as you suggested, you cannot use an array as the _id value. However please be aware that once a document is created the _id becomes unchangeable. This means that using the _id field as a meaningful index of geospatial data may not be of much value.
Have a look here for more info on the _id field and here for some information about creating geospatial indexes in Mongo