MongoDB / Mongoose查询子文档和索引

时间:2017-10-02 17:14:20

标签: mongodb indexing mongoose mongoose-schema

好的,所以我是MongoDB和基于文档的数据库的新手。我在MongoDB中存储了一个包含两个子文档的配置文件集合; '关键词'和国家'。以下是架构信息:

var Country = new mongoose.Schema({
    name: String,
    countryCode: String
});

var Interest = new mongoose.Schema({
    label: {
        type: String
    }
});

var Profile = connection.model('profile', {
    uid: {
        type: String,
        unique: true,
        sparse: true
    },
    username: String
    country: Country,
    interests: [Interest],
    member_since: Date
});

假设我希望能够运行快速有效的查询 ,以便我可以选择所有感兴趣的用户。音乐'并且其countryCode是' AU'但是以一种不会扫描所有文件的有效方式完成(我猜我需要一个索引?),我该怎么做?以下是Compass中显示的示例配置文件:

_id:59d17efa3ed3a453e2b865f9
username:"Rudolph"
country:Object
  name: "Australia"
  countryCode:"AU"
  _id:59d17efa3ed3a453e2b865fa
__v:0
interests:Object
  label:Array
    0:"music"
    1:"film"
    2:"dance"
member_since:2017-10-01 19:49:14.565

0 个答案:

没有答案