我创建了一个带有全名字段的用户集合(即Jose Osorio,Jose castro,John smith,Maria Smith),我需要创建一个搜索栏来查找注册用户的姓名或姓氏.
即写在搜索栏jose,我想看到何塞奥索里奥和何塞卡斯特罗.
我在数据库中读到了关于创建索引但是它没有用,或者我做错了,我该怎么做才能解决这个问题?
要添加索引:
Meteor.startup(function () {
Meteor.users._ensureIndex({ "fullname": 1});
});
Run Code Online (Sandbox Code Playgroud)
并制作选择器,请查看:https: //atmospherejs.com/fourq/typeahead
你也可以rawCollection
像这样使用:
Products.rawCollection().createIndex({
"type": "text",
"searchString": "text",
"title": "text",
"brand": "text",
"description": "text"
}, {
"weights": {
type: 5,
brand: 4,
title: 3,
searchString: 3,
description: 1
}
});
Run Code Online (Sandbox Code Playgroud)
您可以使用例如db.books.createIndex( { "category": 1 } )
mongo shell 来完成此操作。