有没有办法以与mongoshell相同的格式执行mongo查询或js脚本?
示例查询:
db.getCollection('MyCollection').aggregate([
{
"$unwind": "$rootElement.list"
},
{
"$match": {$expr: {$eq: ["$rootElement.list.elementId", "$rootElement.elementId"]}}
}
]
我只想把这个查询作为字符串或query. js文件,并从java代码执行。
在4.2版中,MongoDB删除了val命令,但还有其他选择吗?谢谢。
请参阅以下代码
BsonArray bsonValues = BsonArray.parse("script")
LinkedList<BsonDocument> documents = bsonValues.stream().map(x -> x.asDocument()).collect(Collectors.toCollection(LinkedList::new))
MongoCursor<Document> iterator = collection.aggregate(documents).iterator();
if(iterator.hasNext()){
return IteratorUtils.toList(iterator);
}