【问题标题】:2d Index on MongoDB with nested object带有嵌套对象的 MongoDB 上的 2d 索引
【发布时间】:2017-02-16 22:42:53
【问题描述】:

我有一个像这样的 mongoDB 架构:

var PlumberSchema = new Schema({
        firstname: {
            type: String
        },
        ...
        office: {
            address:{ type:String },
            location: { type:[Number], index: '2d'}
        }
        ...
 }

index:'2d' 是我需要为地理空间搜索创建 2D 索引的位置。

为了做到这一点,我已经阅读了文档:https://docs.mongodb.com/manual/tutorial/build-a-2d-index/,并且为了方便我报告了一个示例:

db.<collection>.createIndex( {<location field> : "<index type>"}

如果我的 Schema 是这样的,那对我有帮助:

var PlumberSchema = new Schema({
        firstname: {
            type: String
        },
        ...
        address:{ type:String },
        location: { type:[Number], index: '2d'}
        ...
 }

要在这种情况下创建索引,我会写:

db.<collection>.createIndex( {location:"2d"})

但正如你所见,我有一个 嵌套对象。我该怎么办?

【问题讨论】:

    标签: mongodb mongoose mongodb-query geospatial mean-stack


    【解决方案1】:

    试试这个:

    db.<collection>.createIndex( {"office.location":"2d"})
    

    【讨论】:

    • 试过了。它不起作用。然后我尝试使用以下命令执行查询:db..find({ office: { location: { $near: [11.255814, 43.769560], $maxDistance: 0.10 } } }),但没有得到任何结果。跨度>
    • 此查询是针对2dsphere 索引(docs.mongodb.com/manual/core/2dsphere),maxDistance 以米为单位,因此您查询的是 10 厘米,不确定是否应该返回任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多