進階Mongo
1.操作Mongo的Array
https://docs.mongodb.com/manual/reference/operator/update/pull/#up._S_pull
Ex: 以下可把rating_my Array中的item物件中的_id與req.body.item._id 值相同的所有欄位移除
User.update({_id: req.token.data._id}, {$pull: { //先把舊的移除
rating_my: {
"item._id": req.body.item._id
}
}})2.分頁快速query
http://stackoverflow.com/questions/7228169/slow-pagination-over-tons-of-records-in-mongo
3.使用Geo search
https://docs.mongodb.com/manual/reference/operator/query-geospatial/
以下使用$near為範例
1.先在schema建立index
var c = new mongoose.Schema({
status: String, //物品承租階段狀態 eg,尋租中,已出租,已還租
rented: Boolean, //是否已出租
renterInfo: Object, //承租者相關資訊,填表
lessee: String, //承租者
geometry: {
type: {
type: String,
default: "Point"
},
coordinates: [Number]
}, //物品所在地
regionName: String,
})
c.index({geometry: '2dsphere'});
exports.Test = mongoose.model('test', c)建立後可以查看到index
2.之後即可搜尋
3.另外要記得coordinates 是先放Longitude才放Latitude,跟一般google地圖相反
單位為公尺
4.新增欄位
新增欄位的意思為在每個document新增一筆資料。
後面兩個參數分別為:
https://stackoverflow.com/a/7714428
5.刪除 DB 資料
不會刪除 DB內的 USER
6. Aggregate
用來進階版 query 資料的方法,可組合出許多進階技。
範例: 取得某段時間內的資料累計 (範例資料以小時為單位)

三個月為一組資料加總
Last updated
Was this helpful?