Reputation: 5139
Let's say I have a MongoDB document like this:
{
"_id" : ObjectId("xxxx-xxxx"),
"name" : "judge dredd",
"docs" : {
"readme-1" : "Please do!",
"readme-2" : "Go ahead...",
"readme-n" : "foo bar"
}
}
How may I create a text index that can index and search any of the values of the object docs
, but nothing else?
The issues that that the keys of the docs
document may be anything, and I don't know what they are before hand.
The MongoDB docs mention a wildcard operator:
db.collection.createIndex( { "$**": "text" } )
but that indexes every String including name
which I don't want. The standard way seems to use a dot property:
db.collection.createIndex( { "docs.readme-1":"text", "docs.readme-2":"text, ... } )
But as mentioned, the object keys may be anything and I don't know them upfront.
If not possible, is there a better way of re-arranging my data document so it is possible?
Upvotes: 0
Views: 40
Reputation: 1140
You could create another model with a reference of that object you want e.g:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const idSchema = new Schema ({
name:{type:string,required:true},
documentReferenced:{Schema.Types.ObjectId, ref:'documentReferenced'}
});
module.exports = mongoose.model('Id ', idSchema);
then when you need to call your documentReferenced or whatever you call it, you just can do the following
Id.findOne({name:req.body.name}).populate('documentReferenced').exec(( err, document)=> {
your code here....
});
I dont'n really know if there is another better way to do what you need but this could be a nice workaround.
Upvotes: 1