java操作mongodb获取记录数,使用MongoDB Java驱动程序通过查询获取每个文档中的字段数...

该博客讨论了如何优化MongoDB Java驱动程序的查询,以仅投影“type1”和“type2”字段并获取文档中存在的字段数。当前代码遍历大量文档并检查每个文档的120种类型,导致性能问题。提出了使用MapReduce方法来减少资源消耗,通过只处理所需字段来提高效率。
摘要由CSDN通过智能技术生成

Is it possible to get the number of fields in document using a Query From MongoDB java Driver

Example:

Document1 :{_id:1,"type1": 10 "type2":30,"ABC":123,"DEF":345}

Document2 :{_id:2,"type2":30,"ABC":123,"DEF":345}

Note: In second document "type1" key doesnt exist .

When i project

Is it possible to project only "type1" and "type2" and get number of fields existing in that document.

With current code i am getting all the documents and individually searching if there is the key i am looking is present in the whole cursor:

The code snipped is as follows:

MongoClient mcl=new MongoClient();

MongoDatabase mdb=mcl.getDatabase("test");

MongoCollection mcol=mdb.getCollection("testcol");

FindIterable findIterable = mcol.find();

MongoCursor cursor = findIterable.iterator();

//Here am having 120 types checking if each type is present..

while(cursor.hasNext())

{

Document doc=cursor.next();

int numberOfTypes=0;

for(int i=1;i<=120;i++)

{

if(doc.containsKey("type"+i))

{

numberOfTypes++;

}

}

System.out.println("*********************************************");

System.out.println("_id"+doc.get("_id"));

System.out.println("Number of Types in this document are "+numberOfTypes);

System.out.println("*********************************************");

}

}

This code is working if the records are less it wont be over load to the application ..Suppose there are 5000000 with each Document containing 120 types , the application is crashing as there would be more garbage collection involved as for every iteration we are creating a document.Is there any other approach through which we can achieve the above stated functionality.

解决方案

From your java code I read

project only "type1" and "type2" and get number of fields existing in that document.

as

project only type[1..120] fields and number of such fields in the document

With this assumption, you can map-reduce it as following:

db.testcol.mapReduce(

function(){

value = {count:0};

for (i = 1; i <= 120; i++) {

key = "type" + i

if (this.hasOwnProperty(key)) {

value[key] = this[key];

value.count++

}

}

if (value.count > 0) {

emit(this._id, value);

}

},

function(){

//nothing to reduce

},

{

out:{inline:true}

});

out:{inline:true} works for small datasets, when result fits into 16Mb limit. For larger responses you need to output to a collection, which you can query and iterate as usual.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值