Meteor js:在用户集合中创建索引

Jos*_*rio 5 javascript meteor

我创建了一个带有全名字段的用户集合(即Jose Osorio,Jose castro,John smith,Maria Smith),我需要创建一个搜索栏来查找注册用户的姓名或姓氏.

即写在搜索栏jose,我想看到何塞奥索里奥和何塞卡斯特罗.

我在数据库中读到了关于创建索引但是它没有用,或者我做错了,我该怎么做才能解决这个问题?

Jer*_*tin 9

要添加索引:

Meteor.startup(function () {  
  Meteor.users._ensureIndex({ "fullname": 1});
});
Run Code Online (Sandbox Code Playgroud)

并制作选择器,请查看:https: //atmospherejs.com/fourq/typeahead

  • _ensureIndex 已弃用。 (2认同)

nil*_*lsi 9

你也可以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)

  • 该解决方案使用 Mongo (createIndex) 中最新的方法并在 Meteor 代码中工作——它应该被接受! (2认同)

Mic*_*elH 3

您可以使用例如db.books.createIndex( { "category": 1 } )mongo shell 来完成此操作。

  • 谢谢为我工作,如果你使用metor,它是meteor mongo db.yourcollection.createIndex({"field": 1}, {unique: true}) (2认同)