Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const Mongoose = require('mongoose');
Mongoose.Promise = Promise;
Mongoose.connect(`mongodb://localhost/test-${Date.now()}`);
const PersonSchema = new Mongoose.Schema({
name: {
first: { type: String },
last: { type: String }
}
});
// Set up first version of the text index.
PersonSchema.index({ 'name.first': 'text' });
const Person = Mongoose.model('Person', PersonSchema);
Person.ensureIndexes()
.then(() => {
// Set up new version of text index.
PersonSchema.index({ 'name.first': 'text', 'name.last': 'text' });
return Person.ensureIndexes();
})
.then(() => {
console.log('It worked.');
process.exit();
})
.catch(error => {
console.error(error.stack);
process.exit(1);
});
{
"name": "mongoose-text-nested-field",
"version": "1.0.0",
"description": "",
"main": "mongoose_text_index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gist.github.com/30c4196e58d47d07499cf6403833b2bd.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/30c4196e58d47d07499cf6403833b2bd"
},
"homepage": "https://gist.github.com/30c4196e58d47d07499cf6403833b2bd",
"dependencies": {
"mongoose": "^4.5.10"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.