简体   繁体   English

Mongodb-在Shell上更新文档之前缺少一些值

[英]Mongodb- some value missing until update the document on shell

I am using java to operate mongodb. 我正在使用Java操作mongodb。

when i queried, find some value is missing.but when i update the document on shell, 当我查询时,发现缺少一些值。但是当我在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 像这样在mongodb中的文档

{
      "_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 Java代码在这里

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. 初始化DBObject您可以使用DBObject.put()将值放入其中,这是正确的。

the your call to findOne() will pull the first document out of your collection. 您对findOne()调用将从您的集合中提取第一个文档。

You need to use db.getCollection("last").update(queryObj) in order to save. 您需要使用db.getCollection("last").update(queryObj)进行保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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