当孙子元素本身是MongoDB中的数组时,如何对孙元素进行索引?

时间:2012-04-13 05:14:35

标签: mongodb

我的文档格式如下所示:

{
    "_id":ObjectId("4fabcd13"),
    users:[
        {id:"001", tags:["friend", "buiz"]}, 
        {id:002, tags:["relationship"]}
    ]
},
{
    "_id":ObjectId("4fabcd14"),
    users:[
        {id:"003", tags:["buiz"]}, 
        {id:002, tags:["friend"]}
    ]
},

我可以让MongoDB索引users.tags元素吗?我曾尝试使用以下语句但失败了:

  

db.invoices.ensureIndex({user.tags:1})

非常感谢!

1 个答案:

答案 0 :(得分:1)

我认为你错过了引号(因为字段名称中的点),该字段被称为“用户”,而不是“用户”:

db.invoices.ensureIndex({"users.tags":1})