2017年8月26日 星期六

python mongodb add item into object

{
    "_id" : ObjectId("59a0ee13a7715608c4877229"),
    "id" : "0050",
    "name" : "元大台灣50",
    "date" : [
        {
            "time" : "20160801",
            "overShares" : "10,407,371",
            "tradingVol" : "1,682",
            "overMoney" : "720,963,537",
            "openPrice" : "68.80",
            "dayHightPrice" : "69.45",
            "dayLowPrice" : "68.80",
            "closePrice" : "69.30",
            "per" : "0.00"
        }
    ]
}

{
    "_id" : ObjectId("59a18587a7715626ec3e6d43"),
    "id" : "0050",
    "name" : "元大台灣50",
    "date" : [
        {
            "time" : "20160801",
            "overShares" : "10,407,371",
            "tradingVol" : "1,682",
            "overMoney" : "720,963,537",
            "openPrice" : "68.80",
            "dayHightPrice" : "69.45",
            "dayLowPrice" : "68.80",
            "closePrice" : "69.30",
            "per" : "0.00",
            "sellStockShortCount" : "148",
            "sellStockShortMoney" : "10256600",
            "stockLendCount" : "0",
            "stockLendMoney" : "0"
        }
    ]
}

code:

collection = self.db[self.collectTitle]
collection.update({'id':stId,'date.time':saveTimeFormat}, {'$set':{'date.$.sellStockShortCount':stSellStockShortCount,
                                                                   'date.$.sellStockShortMoney':stSellStockShortMoney,
                                                                   'date.$.stockLendCount':stStockLendCount,
                                                                   'date.$.stockLendMoney':stStockLendMoney}})

沒有留言:

張貼留言