Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mongo集合中数组字段的有效使用_Java_Arrays_Mongodb_Indexing - Fatal编程技术网

Java Mongo集合中数组字段的有效使用

Java Mongo集合中数组字段的有效使用,java,arrays,mongodb,indexing,Java,Arrays,Mongodb,Indexing,我们在应用程序中使用Mongo DB,在我们的集合中,我们将数组存储为字段。例如: { "_id" : ObjectId("54ef67573848ec32b156b053"), "articleId" : "46384262", "host" : "example.com", "url" : "http://example.com/articleshow/46384262.cms", "publishTime" : NumberLong("1424954

我们在应用程序中使用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"
    ]
}
现在我的情况是需要在标签和CATID数组上创建索引,因为它们是搜索字段。
但是在数组字段上创建索引会极大地增加索引的大小。

你能提出一个更好的方法来实现这一点吗。

你可以用这种方法来重组你的收藏。现在您将拥有3个系列:

Coll1,文档如下所示:

{
   "_id" : ObjectId("54ef67573848ec32b156b053"),
   "articleId" : "46384262",
   ... your other stuff
}
{
      '_id': 1,
      'name': 'wind power'
   }
   {
      '_id': 2,
      'name': 'mytrash'
   }
   ....
标签、文档如下所示:

{
   "_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查询。索引的大小应该更小,但在进行重大更改之前进行测试。

这些并不总是数字,它们也可以是字符串,这就是为什么我将它们用作string@viren知道了。无论如何,我的答案对您的问题有一个可能的解决方案。这不是一个解决方案,但我想您可能会对MongoDB 3.0感兴趣,它目前处于
RC11
状态。有了新的存储引擎,索引可以被压缩(在CPU消耗的权衡下)。@Yaoying我正在等待他们的产品发布日期,到目前为止还没有公布,所以如果可以的话,需要研究其他解决方案