带$ in的mongodb聚合查询的Java代码 [英] Java code for mongodb aggregation query with $in
本文介绍了带$ in的mongodb聚合查询的Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是mongodb的新手,下面需要使用Java中的等效代码
I am new to mongodb and need an equivalent code in java for the below
db.asset.aggregate([{
$unwind : '$asset'
}, {
$match : {
'asset.status' : {
$in : ['1', '2', '3']
},
'asset.siteid' : {
$in : ['123']
}
}
}
]).pretty()
我尝试了以下操作,但没有帮助
I tried the following but it didn't help-out
DBObject unwind = new BasicDBObject("$unwind", "$dp.asset");
DBObject match = BasicDBObjectBuilder.start().push("$match")
.push("dp.asset.status").add("$in", ['ACTIVE', 'LIMITEDUSE', 'OPERATING'])
.push("dp.asset.siteid").add("$in", ['BEDFORD']).get();
AggregationOutput aggr = collection.aggregate(Arrays.asList(unwind, match));
注意:我正在使用mongodb 3.4.1
推荐答案
不确定传递数组值时为什么要使用单引号.
Not sure why you are using single quotes while passing the array value.
传递值的正确语法是
DBObject match = BasicDBObjectBuilder.start().push("$match")
.push("$dp.asset.status").add("$in", Arrays.asList("ACTIVE", "LIMITEDUSE", "OPERATING"))
.push("$dp.asset.siteid").add("$in", Arrays.asList("BEDFORD")).get();
或
DBObject match = BasicDBObjectBuilder.start().push("$match")
.push("$dp.asset.status").add("$in", new String[]{"ACTIVE", "LIMITEDUSE", "OPERATING"})
.push("$dp.asset.siteid").add("$in", new String[]{"BEDFORD"}).get();
对于3.x驱动程序,您应该使用文档.
For 3.x drivers, you should be using Document.
Document unwind = new Document("$unwind", "$dp.asset");
Document match = new Document("$match", new Document("$dp.asset.status", new Document("$in", new String[]{"ACTIVE", "LIMITEDUSE", "OPERATING"})).
append("$dp.asset.siteid", new Document("$in", new String[]{"BEDFORD"})));
Mongo查询代码.
Code for Mongo Query.
Document unwind = new Document("$unwind", "$asset");
Document match = new Document("$match", new Document("$asset.status", new Document("$in", new String[]{"1", "2", "3"})).
append("$asset.siteid", new Document("$in", new String[]{"123"})));
这篇关于带$ in的mongodb聚合查询的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文