如何在mongodb中创建2dsphere索引

时间:2017-06-06 10:06:51

标签: mongodb mongodb-query

我有样本collection

{
    "_id" : ObjectId("5936986f6b5f90330f8e34ba"),
    "factual_id" : "7b758ba0-1e18-012f-0643-0030487f54d8",
    "name" : "Buckhorn Bar",
    "address" : "105 S MAIN St",
    "address_extended" : null,
    "po_box" : "P.O. Box 115",
    "locality" : "Viborg",
    "region" : "SD",
    "post_town" : null,
    "admin_region" : null,
    "postcode" : NumberLong("57070"),
    "country" : "us",
    "tel" : "(605) 766-0086",
    "fax" : null,
    "latitude" : 43.170177,
    "longitude" : -97.081538,
    "neighborhood" : null,
    "website" : null,
    "email" : null,
    "category_ids" : [
        NumberLong("347"),
        NumberLong("312")
    ],
    "category_labels" : [
        [
            "Social",
            "Food and Dining",
            "Restaurants"
        ],
        [
            "Social",
            "Bars"
        ]
    ],
    "chain_name" : null,
    "chain_id" : null,
    "hours" : null,
    "hours_display" : null,
    "existence" : 0.7,
    "cuisine" : [
        "Pub Food"
    ],
    "price" : NumberLong("1"),
    "rating" : null,
    "payment_cashonly" : null,
    "reservations" : null,
    "open_24hrs" : null,
    "founded" : null,
    "attire" : null,
    "attire_required" : null,
    "attire_prohibited" : null,
    "parking" : null,
    "parking_valet" : null,
    "parking_garage" : null,
    "parking_street" : null,
    "parking_lot" : null,
    "parking_validated" : null,
    "parking_free" : null,
    "smoking" : null,
    "meal_breakfast" : true,
    "meal_lunch" : true,
    "meal_dinner" : null,
    "meal_deliver" : null,
    "meal_takeout" : null,
    "meal_cater" : null,
    "alcohol" : true,
    "alcohol_bar" : true,
    "alcohol_beer_wine" : true,
    "alcohol_byob" : null,
    "kids_goodfor" : null,
    "kids_menu" : null,
    "groups_goodfor" : null,
    "accessible_wheelchair" : null,
    "seating_outdoor" : null,
    "wifi" : null,
    "owner" : null,
    "room_private" : null,
    "options_vegetarian" : null,
    "options_vegan" : null,
    "options_glutenfree" : null,
    "options_lowfat" : null,
    "options_organic" : null,
    "options_healthy" : null,
    "loc" : {
        "type" : "Point",
        "coordinates " : [
            -97.081538,
            43.170177
        ]
    }
},

/* 2 createdAt:6/6/2017, 5:26:31 PM*/
{
    "_id" : ObjectId("5936986f6b5f90330f8e34bb"),
    "factual_id" : "7b8a33b9-9ca9-4068-b711-012a77d016a4",
    "name" : "Retro Bean",
    "address" : "10606 Sales Rd S",
    "address_extended" : null,
    "po_box" : null,
    "locality" : "Tacoma",
    "region" : "WA",
    "post_town" : null,
    "admin_region" : null,
    "postcode" : NumberLong("98444"),
    "country" : "us",
    "tel" : "(253) 267-0272",
    "fax" : null,
    "latitude" : 47.160925,
    "longitude" : -122.46548,
    "neighborhood" : null,
    "website" : null,
    "email" : null,
    "category_ids" : [
        NumberLong("342")
    ],
    "category_labels" : [
        [
            "Social",
            "Food and Dining",
            "Cafes, Coffee and Tea Houses"
        ]
    ],
    "chain_name" : null,
    "chain_id" : null,
    "hours" : {
        "sunday" : [
            [
                "7:00",
                "16:00"
            ]
        ],
        "saturday" : [
            [
                "6:00",
                "17:00"
            ]
        ],
        "tuesday" : [
            [
                "5:00",
                "17:30"
            ]
        ],
        "friday" : [
            [
                "5:00",
                "17:30"
            ]
        ],
        "thursday" : [
            [
                "5:00",
                "17:30"
            ]
        ],
        "wednesday" : [
            [
                "5:00",
                "17:30"
            ]
        ],
        "monday" : [
            [
                "5:00",
                "17:30"
            ]
        ]
    },
    "hours_display" : "Mon-Fri 5:00 AM-5:30 PM; Sat 6:00 AM-5:00 PM; Sun 7:00 AM-4:00 PM",
    "existence" : 0.7,
    "cuisine" : null,
    "price" : null,
    "rating" : null,
    "payment_cashonly" : null,
    "reservations" : null,
    "open_24hrs" : null,
    "founded" : null,
    "attire" : null,
    "attire_required" : null,
    "attire_prohibited" : null,
    "parking" : null,
    "parking_valet" : null,
    "parking_garage" : null,
    "parking_street" : null,
    "parking_lot" : null,
    "parking_validated" : null,
    "parking_free" : null,
    "smoking" : null,
    "meal_breakfast" : null,
    "meal_lunch" : null,
    "meal_dinner" : null,
    "meal_deliver" : null,
    "meal_takeout" : null,
    "meal_cater" : null,
    "alcohol" : null,
    "alcohol_bar" : null,
    "alcohol_beer_wine" : null,
    "alcohol_byob" : null,
    "kids_goodfor" : null,
    "kids_menu" : null,
    "groups_goodfor" : null,
    "accessible_wheelchair" : null,
    "seating_outdoor" : null,
    "wifi" : null,
    "owner" : null,
    "room_private" : null,
    "options_vegetarian" : null,
    "options_vegan" : null,
    "options_glutenfree" : null,
    "options_lowfat" : null,
    "options_organic" : null,
    "options_healthy" : null,
    "loc" : {
        "type" : "Point",
        "coordinates " : [
            -122.46548,
            47.160925
        ]
    }
}

现在我尝试使用

loc字段上创建2dsphere索引
db.restaurant.ensureIndex({
    loc : "2dsphere"
});

我也试过

db.restaurant.createIndex( { "loc" : "2dsphere" } )

但在这两种情况下,我都会得到错误" Point必须是数组或对象"

{
    "message" : "Can't extract geo keys: { _id: ObjectId('5936986f6b5f90330f8e34ba'), factual_id: '7b758ba0-1e18-012f-0643-0030487f54d8', name: 'Buckhorn Bar', address: '105 S MAIN St', address_extended: null, po_box: 'P.O. Box 115', locality: 'Viborg', region: 'SD', post_town: null, admin_region: null, postcode: 57070, country: 'us', tel: '(605) 766-0086', fax: null, latitude: 43.170177, longitude: -97.08153799999999, neighborhood: null, website: null, email: null, category_ids: [ 347, 312 ], category_labels: [ [ 'Social', 'Food and Dining', 'Restaurants' ], [ 'Social', 'Bars' ] ], chain_name: null, chain_id: null, hours: null, hours_display: null, existence: 0.7, cuisine: [ 'Pub Food' ], price: 1, rating: null, payment_cashonly: null, reservations: null, open_24hrs: null, founded: null, attire: null, attire_required: null, attire_prohibited: null, parking: null, parking_valet: null, parking_garage: null, parking_street: null, parking_lot: null, parking_validated: null, parking_free: null, smoking: null, meal_breakfast: true, meal_lunch: true, meal_dinner: null, meal_deliver: null, meal_takeout: null, meal_cater: null, alcohol: true, alcohol_bar: true, alcohol_beer_wine: true, alcohol_byob: null, kids_goodfor: null, kids_menu: null, groups_goodfor: null, accessible_wheelchair: null, seating_outdoor: null, wifi: null, owner: null, room_private: null, options_vegetarian: null, options_vegan: null, options_glutenfree: null, options_lowfat: null, options_organic: null, options_healthy: null, loc: { type: 'Point', coordinates : [ -97.08153799999999, 43.170177 ] } }  Point must be an array or object",
    "stack" : "MongoError: Can't extract geo keys: { _id: ObjectId('5936986f6b5f90330f8e34ba'), factual_id: '7b758ba0-1e18-012f-0643-0030487f54d8', name: 'Buckhorn Bar', address: '105 S MAIN St', address_extended: null, po_box: 'P.O. Box 115', locality: 'Viborg', region: 'SD', post_town: null, admin_region: null, postcode: 57070, country: 'us', tel: '(605) 766-0086', fax: null, latitude: 43.170177, longitude: -97.08153799999999, neighborhood: null, website: null, email: null, category_ids: [ 347, 312 ], category_labels: [ [ 'Social', 'Food and Dining', 'Restaurants' ], [ 'Social', 'Bars' ] ], chain_name: null, chain_id: null, hours: null, hours_display: null, existence: 0.7, cuisine: [ 'Pub Food' ], price: 1, rating: null, payment_cashonly: null, reservations: null, open_24hrs: null, founded: null, attire: null, attire_required: null, attire_prohibited: null, parking: null, parking_valet: null, parking_garage: null, parking_street: null, parking_lot: null, parking_validated: null, parking_free: null, smoking: null, meal_breakfast: true, meal_lunch: true, meal_dinner: null, meal_deliver: null, meal_takeout: null, meal_cater: null, alcohol: true, alcohol_bar: true, alcohol_beer_wine: true, alcohol_byob: null, kids_goodfor: null, kids_menu: null, groups_goodfor: null, accessible_wheelchair: null, seating_outdoor: null, wifi: null, owner: null, room_private: null, options_vegetarian: null, options_vegan: null, options_glutenfree: null, options_lowfat: null, options_organic: null, options_healthy: null, loc: { type: 'Point', coordinates : [ -97.08153799999999, 43.170177 ] } }  Point must be an array or object" +
              "at Function.MongoError.create (C:\\Users\\aman.maurya\\AppData\\Local\\mongobooster\\app-3.5.6\\resources\\app.asar\\node_modules\\mongodb-core\\lib\\error.js:31:11)" +
              "at C:\\Users\\aman.maurya\\AppData\\Local\\mongobooster\\app-3.5.6\\resources\\app.asar\\node_modules\\mongodb-core\\lib\\connection\\pool.js:497:72" +
              "at authenticateStragglers (C:\\Users\\aman.maurya\\AppData\\Local\\mongobooster\\app-3.5.6\\resources\\app.asar\\node_modules\\mongodb-core\\lib\\connection\\pool.js:443:16)" +
              "at Connection.messageHandler (C:\\Users\\aman.maurya\\AppData\\Local\\mongobooster\\app-3.5.6\\resources\\app.asar\\node_modules\\mongodb-core\\lib\\connection\\pool.js:477:5)" +
              "at Socket.<anonymous> (C:\\Users\\aman.maurya\\AppData\\Local\\mongobooster\\app-3.5.6\\resources\\app.asar\\node_modules\\mongodb-core\\lib\\connection\\connection.js:240:18)" +
              "at emitOne (events.js:96:13)" +
              "at Socket.emit (events.js:188:7)" +
              "at readableAddChunk (_stream_readable.js:176:18)" +
              "at Socket.Readable.push (_stream_readable.js:134:10)" +
              "at TCP.onread (net.js:551:20)",
    "name" : "MongoError",
    "ok" : 0,
    "errmsg" : "Can't extract geo keys: { _id: ObjectId('5936986f6b5f90330f8e34ba'), factual_id: '7b758ba0-1e18-012f-0643-0030487f54d8', name: 'Buckhorn Bar', address: '105 S MAIN St', address_extended: null, po_box: 'P.O. Box 115', locality: 'Viborg', region: 'SD', post_town: null, admin_region: null, postcode: 57070, country: 'us', tel: '(605) 766-0086', fax: null, latitude: 43.170177, longitude: -97.08153799999999, neighborhood: null, website: null, email: null, category_ids: [ 347, 312 ], category_labels: [ [ 'Social', 'Food and Dining', 'Restaurants' ], [ 'Social', 'Bars' ] ], chain_name: null, chain_id: null, hours: null, hours_display: null, existence: 0.7, cuisine: [ 'Pub Food' ], price: 1, rating: null, payment_cashonly: null, reservations: null, open_24hrs: null, founded: null, attire: null, attire_required: null, attire_prohibited: null, parking: null, parking_valet: null, parking_garage: null, parking_street: null, parking_lot: null, parking_validated: null, parking_free: null, smoking: null, meal_breakfast: true, meal_lunch: true, meal_dinner: null, meal_deliver: null, meal_takeout: null, meal_cater: null, alcohol: true, alcohol_bar: true, alcohol_beer_wine: true, alcohol_byob: null, kids_goodfor: null, kids_menu: null, groups_goodfor: null, accessible_wheelchair: null, seating_outdoor: null, wifi: null, owner: null, room_private: null, options_vegetarian: null, options_vegan: null, options_glutenfree: null, options_lowfat: null, options_organic: null, options_healthy: null, loc: { type: 'Point', coordinates : [ -97.08153799999999, 43.170177 ] } }  Point must be an array or object",
    "code" : 16755
}

请让我做错了

0 个答案:

没有答案