也就是类似sql中的where 字段A=字段B这样的条件,我查了下网上有些帖子中写的用$where的方式我查询的时候会出错,类似于:new Document("$where",new Document("字段A", "字段B")),驱动版本是3.0
在网上查到了一些资料用法,根据这些方法写了如下代码:
`String ageStr = "function (){return 字段A==字段B};";
Document cond = new Document("$where",ageStr);
FindIterable iterable=mongodao.find(cond);`
查询的时候并没有报错,但是用iterable.forEach(new Block()
循环的时候报错,请教应该怎么写才对?
另外mongodb的java开发文档哪里有比较详细的,官网上的例子实在太简单了没有更多方法的使用说明
首先在mongo shell 里面是 这样查询的
`db.ttt.find({})
{ "_id" : ObjectId("566e707540b73d11c02cd058"), "a" : 1, "b" : 1, "c" : 3 }
db.ttt.find({$where:"this.a == this.b"})
{ "_id" : ObjectId("566e707540b73d11c02cd058"), "a" : 1, "b" : 1, "c" : 3 }`
转化到 java里面Document cond = new Document("$where","this.a == this.b");
如果用 function的话 其实是写js代码
function a(){
if(this.a == this.b)
return true
return false
}
也就是写成 Document cond = new Document("$where","function a(){if(this.a == this.b){return true}return false}");