mongo中的or查询如何使用

mysql使用or

select * from user where name = '张三' or name = '李四';

这样会查询出张三和李四

mongo中使用or

db.collection.find {{ $or: [{ name: "张三" }, { name: "李四" }] } 
}

or接收的是一个数组
如果使用 MongoTemplate 的话应该这样用:

Criteria().orOperator(Criteria.where("name").isEqualsTo("张三"),Criteria.where("name").isEqualsTo("李四")
)

orOperator 的源码,orOperator方法接收了一个可变长参数,其实就是一个数组,然后将数组内容加到了条件中

public Criteria orOperator(Criteria... criteria) {Assert.notNull(criteria, "Criteria must not be null!");return this.orOperator((Collection)Arrays.asList(criteria));}

this.orOperator((Collection)Arrays.asList(criteria));调用过的是此方法

public Criteria orOperator(Collection criteria) {Assert.notNull(criteria, "Criteria must not be null!");BasicDBList bsonList = this.createCriteriaList(criteria);return this.registerCriteriaChainElement((new Criteria("$or")).is(bsonList));}

使用registerCriteriaChainElement方法注册or查询


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部