๊ธฐ๋ฅ ์ ์์ฒญํ๊ฑฐ๋ ๋ฒ๊ทธ๋ฅผ ๋ณด๊ณ ํ์๊ฒ ์ต๋๊น?
๊ฐ๋ฅํ ๋ฒ๊ทธ
ํ์ฌ ํ๋์ ๋ฌด์์ ๋๊น?
์ง๋ฆฌ ๊ณต๊ฐ์ ์ขํ๋ฅผ ์ ์ฅํ ์ ์์ต๋๋ค
์ฌ๊ธฐ์ ์์ ๋ฏธ์ํ์ง๋ง ํด๊ฒฐ์ฑ ์ ์ฐพ์ ์ ์์ต๋๋ค.
์ด๊ฒ์ ๋ด ์คํค๋ง์ ๋๋ค.
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 ๋ฒ์ ์ ์ธ๊ธํ์ญ์์ค.
์ต์
์ ๋ฌธ์ ๋ฅผ ๋ง๋ค ๋ ์ฐธ์กฐ๋ ๊ธฐ๊ณ ์ง์นจ์์ ์ธ์ฉ:
๋ฒ๊ทธ ๋ณด๊ณ ์๊ฐ ์๋ 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 ๊ฐ์ฌํฉ๋๋ค...
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. ๋์์ด ๋๊ธธ ๋ฐ๋๋๋ค.