Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor js: Create index in user collection

I create a user collection with a fullname field (i.e. Jose Osorio, Jose castro, John smith, Maria Smith), I need to create a search-bar to find registered users by their name or last name.

I.e. write in the search-bar jose and I want to see Jose Osorio and Jose castro.

I read about create Index in the database but it did not work or I did that wrong, what can i do to solve this?

like image 538
Jose Osorio Avatar asked Sep 13 '15 14:09

Jose Osorio


Video Answer


3 Answers

You can also use rawCollection like this:

Products.rawCollection().createIndex({
    "type": "text",
    "searchString": "text",
    "title": "text",
    "brand": "text",
    "description": "text"
}, { 
    "weights": {
        type: 5,
        brand: 4,
        title: 3,
        searchString: 3,
        description: 1
    }
});
like image 139
nilsi Avatar answered Oct 06 '22 23:10

nilsi


To add the index :

Meteor.startup(function () {  
  Meteor.users._ensureIndex({ "fullname": 1});
});

and to make the picker, have a look at : https://atmospherejs.com/fourq/typeahead

like image 9
Jerome Martin Avatar answered Oct 07 '22 00:10

Jerome Martin


You can do it using e.g db.books.createIndex( { "category": 1 } ) from the mongo shell.

like image 3
MichelH Avatar answered Oct 06 '22 22:10

MichelH