Mongo Collection中Array字段的有效使用



我们在应用程序中使用Mongo-DB,在集合中我们将数组存储为字段。例如:

{
    "_id" : ObjectId("54ef67573848ec32b156b053"),
    "articleId" : "46384262",
    "host" : "example.com",
    "url" : "http://example.com/articleshow/46384262.cms",
    "publishTime" : NumberLong("1424954100000"),
    "tags" : [
            "wind power",
            "mytrah",
            "make in india",
            "government",
            "andhra pradesh"
    ],
    "catIds" : [
            "2147477890",
            "13352306",
            "13358350",
            "13358361"
    ]
}

现在我的情况是需要在标签和catIds数组上创建索引,因为它们是搜索字段
但是在数组字段上创建索引会极大地增加索引的大小
你能建议一个更好的方法来实现这一点吗。

您可以用这种方式重组您的集合。现在你将有3个集合:

Coll1,文档如下:

{
   "_id" : ObjectId("54ef67573848ec32b156b053"),
   "articleId" : "46384262",
   ... your other stuff
}

标签,文件看起来像这样:

{
      '_id': 1,
      'name': 'wind power'
   }
   {
      '_id': 2,
      'name': 'mytrash'
   }
   ....

以及一个将coll1链接到标签的集合:

{
   "collID" : ObjectId("54ef67573848ec32b156b053"),
   "tagID": 1
}
{
   "collID" : ObjectId("54ef67573848ec32b156b053"),
   "tagID": 2
}

Mongo没有联接,所以您需要在应用程序层上进行联接。这将需要3个mongo查询。索引的大小应该更小,但在进行重大更改之前进行测试。

最新更新