简体   繁体   中英

Mongodb- some value missing until update the document on shell

I am using java to operate mongodb.

when i queried, find some value is missing.but when i update the document on shell,

var dataList = db.last.find({"date":"2016-03-09"});
    for(var i = 0;i < dataList.length();i++){
        var data = dataList[i];   //do nothing with it
        db.last.update({"_id":data._id},data);
    }

and then queried,the result is normal. the document in mongodb like that

{
      "_id" : ObjectId("56df0865d23b548768ae4256"),
      "uniq_key" : "last:HIS_AI:13:20:2016-3-9",
      "type" : "last",
      "date" : "2016-03-09",
      "channel_id" : "13",
      "no_ai" : "20",
      "zset" : [{
          "hour" : "0",
          "dataMin" : [["195177600", "7407.899902", "0", "195177900", "7407.899902", "0", "195178200", "7408.000000", "0", "195178500", "7408.000000", "0", "195178800", "7408.000000", "0", "195179100", "7408.100098", "0", "195179400", "7408.100098", "0", "195179700", "7408.100098", "0", "195180000", "7408.200195", "0", "195180300", "7408.200195", "0", "195180600", "7408.200195", "0", "195180900", "7408.200195", "0"]]
        }, {
          "hour" : "1",
          "dataMin" : [["195181200", "7408.299805", "0", "195181500", "7408.299805", "0", "195181800", "7408.299805", "0", "195182100", "7408.399902", "0", "195182400", "7408.399902", "0", "195182700", "7408.399902", "0", "195183000", "7408.399902", "0", "195183300", "7408.500000", "0", "195183600", "7408.500000", "0", "195183900", "7408.500000", "0", "195184200", "7408.600098", "0", "195184500", "7408.600098", "0"]]
        }, {
          "hour" : "2",
          "dataMin" : [["195184800", "7408.600098", "0", "195185100", "7408.600098", "0", "195185400", "7408.700195", "0", "195185700", "7408.700195", "0", "195186000", "7408.700195", "0", "195186300", "7408.799805", "0", "195186600", "7408.799805", "0", "195186900", "7408.799805", "0", "195187200", "7408.899902", "0", "195187500", "7408.899902", "0", "195187800", "7408.899902", "0", "195188100", "7408.899902", "0"]]
        }, {
          "hour" : "3",
          "dataMin" : [["195188400", "7409.000000", "0", "195188700", "7409.000000", "0", "195189000", "7409.000000", "0", "195189300", "7409.100098", "0", "195189600", "7409.100098", "0", "195189900", "7409.100098", "0", "195190200", "7409.100098", "0", "195190500", "7409.200195", "0", "195190800", "7409.200195", "0", "195191100", "7409.200195", "0", "195191400", "7409.299805", "0", "195191700", "7409.299805", "0"]]
        }]
    }

the missing result is

{
        "_id": {
            "$oid": "56df0865d23b548768ae4256"
        },
        "uniq_key": "last:HIS_AI:13:20:2016-3-9",
        "type": "last",
        "date": "2016-03-09",
        "channel_id": "13",
        "no_ai": "20",
        "zset": [
            {
                "hour": "0",
                "dataMin": [
                    [
                        "0"    //**bad value**
                    ]
                ]
            },
            {
                "hour": "1",
                "dataMin": [
                    [
                        "0"
                    ]
                ]
            },
            {
                "hour": "2",
                "dataMin": [
                    [
                        "0"
                    ]
                ]
            }
        ]
    }

the java code is here

MongoClient mongoClient = new MongoClient("10.3.1.30");
    DB db = mongoClient.getDB("energy");

    DBObject queryObj = new BasicDBObject();
    queryObj.put("channel_id", "13");
    queryObj.put("no_ai", "20");
    queryObj.put("date", "2016-03-09");

    DBObject result = db.getCollection("last").findOne(queryObj );
    System.out.println(result);

Help please!!!

When you initialize your DBObject you then put values into it using DBObject.put() this is correct.

the your call to findOne() will pull the first document out of your collection.

You need to use db.getCollection("last").update(queryObj) in order to save.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM