java - mongodb 查询结果 - 将值存储在 jsonObject 中

标签 java json mongodb mongodb-query mongodb-java

我正在 mongodb 中执行查询,给出以下结果:

    {
        "_id" : "180",
        "total" : 1
    }, 
    {
        "_id" : "181",
        "total" : 1
    }, 
    {
        "_id" : "182",
        "total" : 29
    }

这里我将 _id 的上限设置为 186,将下限设置为 180。

因此,查询搜索 _id 并传递值。

但我想按如下方式存储结果。缺少的 _id 应存储 0。

{
    "_id" : "180",
    "total" : 1
}, 
{
    "_id" : "181",
    "total" : 1
}, 
{
    "_id" : "182",
    "total" : 29
},
{
    "_id" : "183",
    "total" : 0
},
{
    "_id" : "184",
    "total" : 0
},
{
    "_id" : "185",
    "total" : 0
},
{
    "_id" : "186",
    "total" : 0
}

我尝试通过以下方式在 Java 中获取结果后插入值。

dayST和daySTP是_id的上限和下限。

output = table.aggregate( pipeline );
   for ( final DBObject res : output.results() ) 
      {
        for (;dayST<daySTP;dayST++)
            {
             int _id1 = Integer.parseInt(res.get("_id").toString());
                if(dayST == _id1)
                 {
                   data.add( new Gson().fromJson( res.toString(), JsonObject.class ) );


                 }
                else
                {       
                final JsonObject dataForNoResults = new JsonObject();
                dataForNoResults.addProperty( "_id", dayST );
                dataForNoResults.addProperty( "totalcount", 0 );
                data.add( dataForNoResults );

                }

             }


       } 

但是,它给出了以下输出:

 {"_id":"180","totalcount":1},
 {"_id":181,"totalcount":0},
 {"_id":182,"totalcount":0},
 {"_id":183,"totalcount":0},
 {"_id":184,"totalcount":0},
 {"_id":185,"totalcount":0},
 {"_id":186,"totalcount":0}

我知道我的循环中的逻辑不正确。有人可以指出吗?

编辑: 我用这个解决了它:

ArrayList<Integer> ar = new ArrayList<Integer>();
    ArrayList<Integer> br = new ArrayList<Integer>();

output = table.aggregate( pipeline );
  for ( final DBObject res : output.results() ) 
     {
      data.add( new Gson().fromJson( res.toString(), JsonObject.class ) );
      ar.add(Integer.parseInt(res.get("_id").toString()));                              
       }
                    while(dayST<daySTP)
                 { 
                        if(!ar.contains(dayST)){
                            br.add(dayST);}
                        dayST++;
                 }
                    for(int i = 0;i< br.size();i++)
                    {
                        final JsonObject dataForNoResults = new JsonObject();
                        dataForNoResults.addProperty( "_id", br.get(i) );
                        dataForNoResults.addProperty( "totalcount", 0 );
                        data.add( dataForNoResults );
                    }

有更好的解决方案吗?

最佳答案

这是一个循环问题。

while (dayST<daySTP)
{
    for ( final DBObject res : output.results() ) 
    {
        if(dayST == _id1)
        {
            data.add( new Gson().fromJson( res.toString(), JsonObject.class ) );
        }
        else
        {       
           JsonObject dataForNoResults = new JsonObject();
           dataForNoResults.addProperty( "_id", dayST );
           dataForNoResults.addProperty( "totalcount", 0 );
           data.add( dataForNoResults );
        } 
        dayST++; 
    }
}

关于java - mongodb 查询结果 - 将值存储在 jsonObject 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31239362/

相关文章:

java - 如何使用方法引用在构造函数中创建的数据类型(数组)

java - Hadoop 总是以 java.util.concurrent.TimeoutException 结束

java - 安卓 : AlertDialog doesn't show

javascript - Lodash:通过多个属性过滤嵌套对象

bash - mongodb安装失败

java - Primefaces 数据表 - 更改字体大小

java - 如何迭代 JSONObject 的 N 层?

javascript - 为什么我的fs错误在Node.js中返回空对象?

javascript - “存储” Mongoose 模式/模型(不是文档)以供将来使用

javascript - 在 Mongoose 模式中添加带有破折号字符的字段