标签: mongodb-indexes

嵌入式文档中的mongo db索引

我有一个域对象模型如下...

@document
Profile
{
     **social profile list:**
      SocialProfile
      {     
          **Interest list:**
          {
           Interest
            {
                 id
                 type
                 value
            }
            ...
       }
       ...
}
Run Code Online (Sandbox Code Playgroud)

每个简档可以具有许多社交简档,在每个社交简档中,通过特定社交简档(社交简档表示像Facebook这样的社交网络)有许多与简档相关的兴趣,每个兴趣也是具有字段id,类型,值的嵌入文档.

所以我有两个问题..我可以在嵌入式文档中单独索引几个字段吗?我可以在嵌入式文档中创建复合索引吗?

我想我的模型的复杂性是嵌入式文档的深层次,它是2 ..并且该文档的路径是通过数组......

可以通过元数据注释以spring方式完成吗?如果您认为我的模型有误,请告诉我我是mongo的新手谢谢

arrays indexing spring mongodb mongodb-indexes

5
推荐指数
1
解决办法
7972
查看次数

在聚合框架中使用文本搜索时的 MongoDB 索引优化

我们正在 MongoDB 上构建一个简化版本的搜索引擎。

样本数据集

{ "_id" : 1, "dept" : "tech", "updDate":  ISODate("2014-08-27T09:45:35Z"), "description" : "lime green computer" }
{ "_id" : 2, "dept" : "tech", "updDate":  ISODate("2014-07-27T09:45:35Z"), "description" : "wireless red mouse" }
{ "_id" : 3, "dept" : "kitchen", "updDate":  ISODate("2014-04-27T09:45:35Z"), "description" : "green placemat" }
{ "_id" : 4, "dept" : "kitchen", "updDate":  ISODate("2014-05-27T09:45:35Z"), "description" : "red peeler" }
{ "_id" : 5, "dept" : "food", "updDate":  ISODate("2014-04-27T09:45:35Z"), "description" : "green apple" }
{ "_id" : 6, …
Run Code Online (Sandbox Code Playgroud)

text-search mongodb aggregation-framework mongodb-indexes

5
推荐指数
0
解决办法
768
查看次数

MongoDB:带有数组的文本索引,仅第一项被索引

我有一个具有以下架构的文档

{
  description : String,
  tags : [String]
}
Run Code Online (Sandbox Code Playgroud)

我已将这两个字段都索引为文本,但问题是,每当我在数组中搜索特定字符串时,仅当该字符串是数组的第一个元素时,它才会返回文档。因此,似乎 $text 索引仅适用于第一个元素,这是 mongo 固有的工作方式还是有一个选项必须传递给索引?

示例文档

{
   description : 'random description',
   tags : ["hello", "there"]
}
Run Code Online (Sandbox Code Playgroud)

创建索引的对象

{description : 'text', tags : 'text'}
Run Code Online (Sandbox Code Playgroud)

查询

db.myCollection.find({$text : {$search : 'hello'}});
Run Code Online (Sandbox Code Playgroud)

返回一个文档但是

db.myCollection.find({$text : {$search : 'there'}});
Run Code Online (Sandbox Code Playgroud)

不返回任何内容。

使用版本2.6.11

我还有其他索引,但这些是唯一的文本索引。这是相应的输出db.myCollection.getIndexes()

{
                "v" : 1,
                "key" : {
                        "_fts" : "text",
                        "_ftsx" : 1
                },
                "name" : "description_text_tags_text",
                "ns" : "myDB.myCollection",
                "weights" : {
                        "description" : 1,
                        "tags" : 1
                },
                "default_language" …
Run Code Online (Sandbox Code Playgroud)

mongodb mongodb-query mongodb-indexes

5
推荐指数
1
解决办法
1005
查看次数

过滤和排序 以下哪个查询将使用索引?

有一个people具有以下索引的集合:

{"first_name": 1, "address.state": -1, "address.city": -1, "ssn": 1}
Run Code Online (Sandbox Code Playgroud)

对于过滤和排序,以下哪个查询将使用索引?

  1. { "first_name": { $gt: "J" } }).sort({ "address.city": -1 }
  2. { "first_name": "Jessica" }).sort({ "address.state": 1, "address.city": 1 }
  3. { "first_name": "Jessica", "address.state": { $lt: "S"} }).sort({ "address.state": 1 }
  4. {"address.city":"West Cindy"}).sort({ "address.city":-1}
  5. {"address.state":"South Dakota","first_name": "Jessica"}).sort({ "address.city":-1}

我已经解决了以下问题: 以下哪些查询将使用索引?

但它只解释了过滤的索引,我需要对过滤器和排序功能使用索引。

另外,如何确定索引是否用于过滤和排序或不使用?

sorting filter mongodb mongodb-query mongodb-indexes

5
推荐指数
1
解决办法
8125
查看次数

Spring Boot MongoDB 索引为 expireAfterSeconds 以自动删除文档不起作用

我对 MongoDB 中的“生存时间”设置有疑问。我在我的实体中的 Spring-Boot 2.0.2.RELEASE 项目中创建了一个索引注释,它代表我在 MongoDB 中的文档。我将测试的“expireAfterSeconds”设置为 15 秒,但 MongoDB 在 15 秒后不会删除插入的文档。有人能告诉我我做错了什么吗?

这是 JSON 形式的 MongoDB 索引:

[
  2,
  {
    "createdDateTime" : 1
  },
  "deleteAt",
  "AccountServiceDB.AccountRegistration",
  NumberLong(15)
]
Run Code Online (Sandbox Code Playgroud)

这是我的实体:

@Document(collection = "AccountRegistration")
public class UserRegistration {

  @Id
  private ObjectId _id;
  @Indexed(unique = true)
  private String username;

  @Indexed(unique = true)
  private String email;

  private String user_password;

  @Indexed(name = "deleteAt", expireAfterSeconds = 15)
  private Date createdDateTime;

  public UserRegistration() {}

  public ObjectId get_id() {
    return _id;
  }

  public void set_id(ObjectId _id) …
Run Code Online (Sandbox Code Playgroud)

spring mongodb mongodb-indexes spring-boot spring-mongodb

5
推荐指数
1
解决办法
2003
查看次数

针对不同的排序和过滤条件应该创建哪些MongoDB索引来提高性能?

我有大约 100,000,000 条记录的 MongoDB 集合。

\n\n

在网站上,用户使用“细化搜索”功能搜索这些记录,他们可以按多个条件进行过滤:

\n\n
    \n
  • 按国家、州、地区;
  • \n
  • 按价格范围;
  • \n
  • 按行业分类;
  • \n
\n\n

此外,他们还可以查看排序的搜索结果:

\n\n
    \n
  • 按标题(升序/降序),
  • \n
  • 按价格(升/降),
  • \n
  • 通过 bestMatch 字段。
  • \n
\n\n

我需要创建索引以避免对上述任何组合进行全面扫描(因为用户使用大多数组合)。遵循相等排序范围规则,我必须创建很多索引:

\n\n

所有过滤器组合 \xc3\x97 所有排序 \xc3\x97 所有范围过滤器,如下所示:

\n\n
country_title\nstate_title\nregion_title\ntitle_price\nindustry_title\ncountry_title_price\ncountry_industry_title\nstate_industry_title\n...\ncountry_price\nstate_price\nregion_price\n...\ncountry_bestMatch\nstate_bestMatch\nregion_bestMatch\n...\n
Run Code Online (Sandbox Code Playgroud)\n\n

事实上,我有更多的标准(包括相等和范围),以及更多的排序。例如,我有多个价格字段,用户可以按任何价格排序,因此我必须为每个价格字段创建所有过滤索引,以防用户按该价格排序。

\n\n

我们使用 MongoDB 4.0.9,目前只有一台服务器。

\n\n

在我进行排序之前,这会更容易,至少我可以有一个复合索引,例如country_state_region当搜索某个地区时,并且始终在查询中包含国家/地区和州。但是在最后有排序字段,我不能再这样做了 - 即使对于位置(国家/州/地区),我也必须使用所有排序组合创建所有不同的索引。

\n\n

另外,并不是所有的产品都有价格,所以我不能只按price字段排序。相反,我必须创建两个索引:{hasPrice: -1, price: 1}{hasPrice: -1, price: -1}(此处,hasPrice 为 -1,无论价格排序方向如何,始终首先包含 hasPrice=true 的记录)。

\n\n

目前,我使用 NodeJS 代码生成类似于以下内容的索引(这是简化的示例):

\n\n
country_title\nstate_title\nregion_title\ntitle_price\nindustry_title\ncountry_title_price\ncountry_industry_title\nstate_industry_title\n...\ncountry_price\nstate_price\nregion_price\n...\ncountry_bestMatch\nstate_bestMatch\nregion_bestMatch\n...\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,上面的代码生成了 90 多个索引。而在我真正的任务中,这个数字甚至更多。 …

sorting query-optimization mongodb mongodb-indexes

5
推荐指数
1
解决办法
508
查看次数

就mongoDb而言,GeoJSON和旧版坐标对之间有何区别?

我正在尝试使用mongoDb的$ geoNear聚合运算符通过以下方式计算用户到当前位置的距离:

'$geoNear': {
   near: currentLocation,
   distanceField: 'distance',
   spherical: true,
}
Run Code Online (Sandbox Code Playgroud)

与currentLocation类似:

{ "type" : "Point", "coordinates" : [  -122.1575745,  37.4457966 ] }
Run Code Online (Sandbox Code Playgroud)

我的收藏品属于以下类型(使用猫鼬):

users = [{
  ....
  location : {                   // GeoJSON Point or I think it is ;)
            type: { 
                type: String
            },
            coordinates: []
        }
  ....
}]
Run Code Online (Sandbox Code Playgroud)

我正在使用索引(还是猫鼬的语法):

userSchema.index({
  location: '2dsphere'
});
Run Code Online (Sandbox Code Playgroud)

现在我面临的问题是,如果我使用上述提到的currentLocation(以GeoJSON的形式)进行查询,我会得到怪异的距离(非常大的数字),但是如果我使用currentLocation.coordinates,即使用旧式坐标对([-122.1575745 ,37.4457966]),我得到正确的结果。但是geoNear的mongoDb文档明确表示,我们可以使用GeoJSON点或旧式坐标对进行查询

我很好奇,想知道GeoJSON点对和传统坐标对之间到底有什么区别?

例如:

{ "_id" : ObjectId("5277679914c6d8f00b000003"), "location" : { "type" : "Point", "coordinates" : [  106.6202887,  -6.1293536 ] } …
Run Code Online (Sandbox Code Playgroud)

mongodb geojson node.js aggregation-framework mongodb-indexes

4
推荐指数
1
解决办法
2944
查看次数

应该为确定大小的小 mongo 集合创建索引吗?

假设我有一个 mongo 集合,它有固定数量的条目,永远不会超过 300-400。例子:

User{
String name;
String phoneNumber;
String address;
String dob;
Integer noOfCars;
}
Run Code Online (Sandbox Code Playgroud)

在这些字段中,我想索引 name 和 phoneNumber。

建议为这样的小集合创建索引吗?这个决定是否完全取决于收藏的规模?它是否取决于我要创建的索引数量?

indexing mongodb mongodb-indexes

4
推荐指数
2
解决办法
555
查看次数

MongoDB 如何选择候选计划

我的应用程序中的查询速度很慢。创建两个索引后,它在本地数据库中使用它们以获得更好的性能。但是当我部署在生产数据库上时,它仍然使用原始索引。

下面是我所做的。

集合中的属性tasksteam_idproject_idcreated_byassignee等。

查询如下所示

db.tasks.find({
  team_id: new ObjectId(teamId),
  $or: [
    {
      project_id: newObjectId(projectId),
      created_by: userId
    },
    {
      assignee: userId
    }
  ]
})
Run Code Online (Sandbox Code Playgroud)

最初只有一个索引team_id,它将检查超过 10k 文档。然后我添加了两个新索引

project_1_created_by_1: {
  project: 1,
  created_by: 1
}

assignee_1: {
  assignee: 1
}
Run Code Online (Sandbox Code Playgroud)

在本地数据库中,我使用explain({ verbose: true }). 我可以看到 MongoDB 评估的索引

[
  QueryOptimizerCursor: [
    'project_1_created_by_1',
    'assignee_1',
  ],
  BtreeCursor: 'team_1'
]
Run Code Online (Sandbox Code Playgroud)

终于QueryOptimizerCursor赢了。

但是当我在生产 MongoDB 上运行它时,结果显示explain({ verbose: true })它只评估了team_1和 …

mongodb mongodb-indexes

4
推荐指数
1
解决办法
1606
查看次数

如何在猫鼬模型上使用 partialFilterExpression

我创建了一个具有电子邮件字段的猫鼬模型。如果用户提供了值,我希望它是唯一的,但如果用户未提供任何值,我希望它为空。我在这里找到了一个很好的 mongodb 参考:https ://docs.mongodb.com/manual/core/index-partial/#partial-index-with-unique-constraints可以工作,但我不知道如何制作在猫鼬上工作

这就是这个领域现在的样子

email: {
    type: String,
    index: true,
    unique: true
  }
Run Code Online (Sandbox Code Playgroud)

如果我保持原样,我将无法使用空/空电子邮件字段创建多个文档

mongoose mongodb mongodb-indexes

4
推荐指数
2
解决办法
3239
查看次数