第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在mongoDB中對(duì)集合記錄中的數(shù)組進(jìn)行排序

如何在mongoDB中對(duì)集合記錄中的數(shù)組進(jìn)行排序

郎朗坤 2019-07-24 20:09:47
如何在mongoDB中對(duì)集合記錄中的數(shù)組進(jìn)行排序MongoDB noob在這里......好的,我有一個(gè)學(xué)生集合,每個(gè)都有一個(gè)如下所示的記錄....我想按照降序排序'類型':'家庭作業(yè)'分?jǐn)?shù)。那個(gè)咒語在mongo shell上是什么樣的?> db.students.find({'_id': 1}).pretty(){         "_id" : 1,         "name" : "Aurelia Menendez",         "scores" : [                 {                         "type" : "exam",                         "score" : 60.06045071030959                 },                 {                         "type" : "quiz",                         "score" : 52.79790691903873                 },                 {                         "type" : "homework",                         "score" : 71.76133439165544                 },                 {                         "type" : "homework",                         "score" : 34.85718117893772                 }         ]}我正在嘗試這個(gè)咒語.... doc = db.students.find()  for (_id,score) in doc.scores:      print _id,score但它不起作用。
查看完整描述

3 回答

?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊

您需要在應(yīng)用程序代碼中操作嵌入式數(shù)組,或在MongoDB 2.2中使用新的聚合框架

mongoshell中的示例聚合:

db.students.aggregate(
    // Initial document match (uses index, if a suitable one is available)
    { $match: {
        _id : 1
    }},

    // Expand the scores array into a stream of documents
    { $unwind: '$scores' },

    // Filter to 'homework' scores 
    { $match: {
        'scores.type': 'homework'
    }},

    // Sort in descending order
    { $sort: {
        'scores.score': -1
    }})

樣本輸出:

{
    "result" : [
        {
            "_id" : 1,
            "name" : "Aurelia Menendez",
            "scores" : {
                "type" : "homework",
                "score" : 71.76133439165544
            }
        },
        {
            "_id" : 1,
            "name" : "Aurelia Menendez",
            "scores" : {
                "type" : "homework",
                "score" : 34.85718117893772
            }
        }
    ],
    "ok" : 1}


查看完整回答
反對(duì) 回復(fù) 2019-07-24
?
長風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

這就是我們用JS和mongo控制臺(tái)解決這個(gè)問題的方法:

db.students.find({"scores.type": "homework"}).forEach(
  function(s){
    var sortedScores = s.scores.sort(
      function(a, b){
        return a.score<b.score && a.type=="homework";
      }
    );
    var lowestHomeworkScore = sortedScores[sortedScores.length-1].score;
    db.students.update({_id: s._id},{$pull: {scores: {score: lowestHomeworkScore}}}, {multi: true});
  })


查看完整回答
反對(duì) 回復(fù) 2019-07-24
?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊

這是java代碼,可用于找出數(shù)組中的最低分?jǐn)?shù)并將其刪除。

public class sortArrayInsideDocument{public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();
    DB db = client.getDB("school");
    DBCollection lines = db.getCollection("students");
    DBCursor cursor = lines.find();
    try {
        while (cursor.hasNext()) {
            DBObject cur = cursor.next();
            BasicDBList dbObjectList = (BasicDBList) cur.get("scores");
            Double lowestScore = new Double(0);
            BasicDBObject dbObject = null;
            for (Object doc : dbObjectList) {
                BasicDBObject basicDBObject = (BasicDBObject) doc;
                if (basicDBObject.get("type").equals("homework")) {
                    Double latestScore = (Double) basicDBObject                            .get("score");
                    if (lowestScore.compareTo(Double.valueOf(0)) == 0) {
                        lowestScore = latestScore;
                        dbObject = basicDBObject;

                    } else if (lowestScore.compareTo(latestScore) > 0) {
                        lowestScore = latestScore;
                        dbObject = basicDBObject;
                    }
                }
            }
            // remove the lowest score here.
            System.out.println("object to be removed : " + dbObject + ":"
                    + dbObjectList.remove(dbObject));
            // update the collection
            lines.update(new BasicDBObject("_id", cur.get("_id")), cur,
                    true, false);
        }
    } finally {
        cursor.close();
    }}}


查看完整回答
反對(duì) 回復(fù) 2019-07-24
  • 3 回答
  • 0 關(guān)注
  • 2858 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)