Mongoose: ์ง€๋ฆฌ์ •๋ณด ์ฟผ๋ฆฌ ์‚ฝ์ž…

์— ๋งŒ๋“  2017๋…„ 12์›” 14์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: Automattic/mongoose

๊ธฐ๋Šฅ ์„ ์š”์ฒญํ•˜๊ฑฐ๋‚˜ ๋ฒ„๊ทธ๋ฅผ ๋ณด๊ณ  ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?

๊ฐ€๋Šฅํ•œ ๋ฒ„๊ทธ

ํ˜„์žฌ ํ–‰๋™์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์ง€๋ฆฌ ๊ณต๊ฐ„์— ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค

์—ฌ๊ธฐ์— ์™€์„œ ๋ฏธ์•ˆํ•˜์ง€๋งŒ ํ•ด๊ฒฐ์ฑ…์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ์€ ๋‚ด ์Šคํ‚ค๋งˆ์ž…๋‹ˆ๋‹ค.

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var userSchema   = new Schema({
  email: {
      type: String,
      required: true,
      unique: true
    },
    password: {type: String, required: true},
    name: {type: String, required: true},
    type: {type: Boolean, default: false, required: true},
    geo: {
      type: { type: String, enum: "Point", default: "Point" },
      coordinates: { type: [ Number ],   default: [ 0,0 ] }
    }
});

userSchema.index({geo: '2dsphere'})
const User = mongoose.model('User', userSchema);
module.exports = User

์ƒˆ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

db.users.insert({email: '[email protected]', password: 'tebrwrw', name: 'teste', type: true, geo: { coordinates: [ 40, 5 ]} })

์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 16755,
                "errmsg" : "Can't extract geo keys: { _id: ObjectId('5a31d79f369885e770a989b7'), email: \"[email protected]\", password: \"tebrwrw\", name: \"teste\", type: true, geo: { coordinates: [ 40.0, 5.0 ] } }  unknown GeoJSON type: { coordinates: [ 40.0, 5.0 ] }"
        }
})

node.js, mongoose ๋ฐ MongoDB ๋ฒ„์ „์„ ์–ธ๊ธ‰ํ•˜์‹ญ์‹œ์˜ค.
์ตœ์‹ 

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋„์›€์ด ๋˜๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

    // in the schma declaration
    'loc': { type: { type: String, enum: "Point", default: "Point" }, coordinates: { type: [Number], default: [0,0] } }

    // after schema declaration*
    schema.index( { loc: "2dsphere" } );

    // and then, how I create it
    model.loc = { type: "Point", coordinates: [newarena.lng, newarena.lat] };

๋ชจ๋“  3 ๋Œ“๊ธ€

์ƒˆ ๋ฌธ์ œ๋ฅผ ๋งŒ๋“ค ๋•Œ ์ฐธ์กฐ๋œ ๊ธฐ๊ณ  ์ง€์นจ์—์„œ ์ธ์šฉ:

๋ฒ„๊ทธ ๋ณด๊ณ ์„œ๊ฐ€ ์•„๋‹Œ Mongoose์— ๋Œ€ํ•œ ์งˆ๋ฌธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ StackOverflow ๋˜๋Š” Gitter ์— ๊ฒŒ์‹œํ•˜์„ธ์š”.

์œ ์ง€ ๊ด€๋ฆฌ์ž๊ฐ€ ์—ฌ๊ธฐ์—์„œ ์„ ๋ณ„ํ•ด์•ผ ํ•˜๋Š” ๋ฒ„๊ทธ/๊ธฐ๋Šฅ์˜ ๋ฐฑ๋กœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋‘˜ ์ค‘ ํ•˜๋‚˜์— ๋Œ€ํ•ด ํ›จ์”ฌ ๋” ๋นจ๋ฆฌ ์‘๋‹ต์„ ๋ฐ›์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋” ํฝ๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋„์›€์ด ๋˜๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

    // in the schma declaration
    'loc': { type: { type: String, enum: "Point", default: "Point" }, coordinates: { type: [Number], default: [0,0] } }

    // after schema declaration*
    schema.index( { loc: "2dsphere" } );

    // and then, how I create it
    model.loc = { type: "Point", coordinates: [newarena.lng, newarena.lat] };

@Inateno ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค...

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰