Hỏi về cách custom field trong mongoose?
-
Ở đây mình có 2 trường commentUpVote và commentDownVote mình muốn sắp xếp giử liệu lấy ra theo công thức là "commentUpVote - commentDownVote" thì sàm như thế vậy mọi người.
//dữ liệu { "_id" : ObjectId("5b23308ac757c0221cfd25a5"), "commentUpVote" : NumberInt(1000), "commentDownVote" : NumberInt(30), "commentContent" : "test user", "commentStatus" : NumberInt(1), "commentImage" : { "type" : null, "url" : null, "video" : null }, "user_ObjectId" : ObjectId("5b1f417ba5290e11d4c39f1f"), "commentCreate" : ISODate("2018-06-15T03:20:42.283+0000"), "commentModify" : ISODate("2018-06-15T03:20:42.283+0000"), "__v" : NumberInt(0) } { "_id" : ObjectId("5b23313fd4640a1464f6f45b"), "commentUpVote" : NumberInt(500), "commentDownVote" : NumberInt(10), "commentContent" : "test noi dung", "commentStatus" : NumberInt(1), "commentImage" : { "type" : null, "url" : null, "video" : null }, "user_ObjectId" : ObjectId("5b1f417ba5290e11d4c39f1f"), "commentCreate" : ISODate("2018-06-15T03:23:43.539+0000"), "commentModify" : ISODate("2018-06-15T03:23:43.539+0000"), "__v" : NumberInt(0) }
//code lấy giữ liệu exports.loadComments = function (req, res) { var trang = req.query.page; var limit = 10; var skip = (trang - 1) * limit; var tempArray = []; var listComment = commentsModel.find({}, null, { skip, limit, sort: { commentUpVote: 1 //Sort by Date Added DESC } }, (err, data) => { if (err) { res.json({ 'error': 'Không kết nối được máy chủ! F5 thử lại' }) } else { var data = ({ trang: parseInt(trang) + 1, comments: data }) res.json(data); } }) }
-
@Kha-Pham Bạn nghiên cứu
virtual fields
trong mongoose đã hỗ trợ nhé. -
@Vũ thank bạn
-
@Vũ Mình tìm ra rồi à aggregate trong mongoose
-
@Kha-Pham Aggregation sẽ đẩy tính toán về cho DB, nhưng có vẻ hợp lý hơn
-
Mình thì sẽ làm get getter function cho model đó.
Ví dụ to_json() function.
Hoặc viết cái get_vote -
@Vũ Aggregation củng giúp gộp bảng nữa nên lấy giữ liệu 2 bảng luôn rất tiện
-
@Nguyen-Hien mình chỉ mới tìm hiểu nên file model của mình khá đơn giản. Củng không hiểu cách bạn muốn truyền đạt như thế nào nữa.
Dưới đây là file model của mình//model comments const mongoose = require('mongoose'); var Schema = mongoose.Schema({ commentContent : {type: String, maxlength: 1000}, commentStatus: Number, // 1 is ok, 2 is ban commentImage: JSON, post_slug: {type: String, required: true, ref: 'posts'}, commentUpVote: {type: Number, default: 0}, commentDownVote: {type: Number, default: 0}, user_ObjectId: {type: mongoose.Schema.Types.ObjectId, ref: 'users'}, commentCreate: {type: Date, default: Date.now}, commentModify: {type: Date, default: Date.now} }) module.exports = mongoose.model('comments', Schema);
-
Schema.methods.toJSON = () => { return { commentContent : this.commentContent, post_slug: this.post_slug, vote: this.commentUpVote - this.commentDownVote ... // Mấy field muốn custom bỏ ra đây rồi xử lí out put. } } hoặc Schema.methods.getVote = () => { return this.commentUpVote - this.commentDownVote }
2 cách đều được. Nhưng cách 1 mình hay dùng. Vì khi mình response của expressjs á. res.status(200).send(JSON.stringify(model)) -> nó sẽ trả cái return toJSON về (mấy framework khác mình k rõ). Cái này tiện cho hidden fields vì 1 số lí do không muốn trả về.
-
@Nguyen-Hien Cám ơn bạn. Mình hiểu rồi