Skip to content

Instantly share code, notes, and snippets.

@djanowski
Last active August 25, 2016 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djanowski/30c4196e58d47d07499cf6403833b2bd to your computer and use it in GitHub Desktop.
Save djanowski/30c4196e58d47d07499cf6403833b2bd to your computer and use it in GitHub Desktop.
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