對列表進行排序並在 mongodb 中插入一個新列 (Sort the list and insert a new column in mongodb)


問題描述

對列表進行排序並在 mongodb 中插入一個新列 (Sort the list and insert a new column in mongodb)

I need to sort states and figure out the highest temperature for each state in this weather data residing in mongodb.

How do I iterate through each record in Javascript and insert a new column 'month_highest' when I figure out the highest temperature for that state ?


Day Time    State   Airport Temperature Humidity WindSpeed
1   54  Vermont BTV 39  57  6
1   154 Vermont BTV 39  57  4
1   254 Vermont BTV 39  57  5
1   354 Vermont BTV 38  70  5
1   454 Vermont BTV 34  92  11

16  53  Florida ORL 46  71  9 16  153 Florida ORL 47  71  8 16  253 Florida ORL 46  73  8 16  353 Florida ORL 47  74  8 16  453 Florida ORL 46  79  7 16  553 Florida ORL 46  79  5 16  653 Florida ORL 46  83  4


參考解法

方法 1:

MongoDB Map‑Reduce

db.temps.mapReduce(
    /* map */
    function() {
        emit( this.State, this.Temperature);
    },

    /* reduce */
    function key(key, values) {
        var ret = 0;
        for (var i=0; i<values.length, i++) {
            if (values[i] > ret) ret = values[i];
        }
        return ret
    },

    /* params */
    {
         query: { 
             DateTime: { 
                 $gte: new Date('2013‑01‑01')
                 ,$lt: new Date('2013‑02‑01') 
             }
         }

         ,sort: {
             State:1
         }
    }
);

As for inserting a new month‑max, that depends on how you want to store it?  How are you querying this data?

You could output the results into a new collection, or use that programmatically to update another collection.  It depends on how you want your data shaped.  It actually looks like you are really flat with your data as it stands.

(by Arvind TMTracker1)

參考文件

  1. Sort the list and insert a new column in mongodb (CC BY‑SA 3.0/4.0)

#mongoDB #node.js






相關問題

將 (.net) BsonDocument 字符串轉換為 (java) DBObject (Converting (.net) BsonDocument string into a (java) DBObject)

Mongo 客戶端出錯 (Error with Mongo Client)

對列表進行排序並在 mongodb 中插入一個新列 (Sort the list and insert a new column in mongodb)

無法導入示例數據集(系統找不到指定文件) (Cannot import example dataset (the system cannot find the specified file))

mongodb c#更新嵌入文檔 (mongodb c# update embedded document)

查詢嵌入列表中的數組 (Querying for array in embedded list)

搜索條件的mongodb map reduce (mongodb map reduce for search criteria)

org.bson.codecs.configuration.CodecConfigurationException:找不到類 [Ljava.lang.String; 的編解碼器; (org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class [Ljava.lang.String;)

在碼頭上部署 gwt Web 應用程序 (deploying gwt web application on jetty)

MongoMetaRecord MegaProtoUser 登錄時密碼未散列 (MongoMetaRecord MegaProtoUser Password not hashed when signing in)

將 mongo shell 連接到受限的 MongoDB Atlas 數據庫? (Connect mongo shell to restricted MongoDB Atlas Database?)

用於 $or 和 $and 場景的 $elemMatch 數組 (Arrays working $elemMatch for $or and $and scenarios)







留言討論