Vietnam

    Nodejs.vn

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Groups
    • Search

    Hỏi về cách custom field trong mongoose?

    ExpressJS/Conect.IO
    0
    10
    818
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Kha Pham
      Kha Pham last edited by

      Ở đâ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);
              }
          })
      }
      

      Đam mê công nghệ
      Không biết nhiều nhưng cũng muốn đóng góp

      Vũ 1 Reply Last reply Reply Quote 0
      • Vũ
        Vũ Global Moderator @Kha Pham last edited by

        @Kha-Pham Bạn nghiên cứu virtual fields trong mongoose đã hỗ trợ nhé.

        Tech-nông
        Email: [email protected]
        Profile: about.me/vunb
        Github: github.com/vunb
        Twitter: twitter.com/nhubaovu

        Kha Pham 2 Replies Last reply Reply Quote 1
        • Kha Pham
          Kha Pham @Vũ last edited by

          @Vũ thank bạn

          Đam mê công nghệ
          Không biết nhiều nhưng cũng muốn đóng góp

          1 Reply Last reply Reply Quote 0
          • Kha Pham
            Kha Pham @Vũ last edited by

            @Vũ Mình tìm ra rồi à aggregate trong mongoose

            Đam mê công nghệ
            Không biết nhiều nhưng cũng muốn đóng góp

            Vũ 1 Reply Last reply Reply Quote 1
            • Vũ
              Vũ Global Moderator @Kha Pham last edited by

              @Kha-Pham Aggregation sẽ đẩy tính toán về cho DB, nhưng có vẻ hợp lý hơn 🙂

              Tech-nông
              Email: [email protected]
              Profile: about.me/vunb
              Github: github.com/vunb
              Twitter: twitter.com/nhubaovu

              Kha Pham 1 Reply Last reply Reply Quote 1
              • Nguyen Hien
                Nguyen Hien last edited by

                Mình thì sẽ làm get getter function cho model đó.
                Ví dụ to_json() function.
                Hoặc viết cái get_vote

                Sent from Rodgers Zone!!!

                Kha Pham 1 Reply Last reply Reply Quote 1
                • Kha Pham
                  Kha Pham @Vũ last edited by

                  @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

                  Đam mê công nghệ
                  Không biết nhiều nhưng cũng muốn đóng góp

                  1 Reply Last reply Reply Quote 0
                  • Kha Pham
                    Kha Pham @Nguyen Hien last edited by

                    @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);
                    

                    Đam mê công nghệ
                    Không biết nhiều nhưng cũng muốn đóng góp

                    Nguyen Hien 1 Reply Last reply Reply Quote 0
                    • Nguyen Hien
                      Nguyen Hien @Kha Pham last edited by

                      @Kha-Pham

                      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ề.

                      Sent from Rodgers Zone!!!

                      Kha Pham 1 Reply Last reply Reply Quote 0
                      • Kha Pham
                        Kha Pham @Nguyen Hien last edited by

                        @Nguyen-Hien Cám ơn bạn. Mình hiểu rồi

                        Đam mê công nghệ
                        Không biết nhiều nhưng cũng muốn đóng góp

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        $(document).ready(function () { app.coldLoad(); }); }