MongoDB的$where查询

有时候,键值对的查询方式并不能满足我们的需求,我们有如下一个集合:

> db.foo.find()
{ “_id” : ObjectId(“4e9165cf717ed94f8289ac0c”), “bar” : “baz”, “count” : 35 }
{ “_id” : ObjectId(“4e916661739f1da5452a4dfe”), “bar” : “bazz”, “count” : 3 }
{ “_id” : ObjectId(“4e9165cf717ed94f8289ac0d”), “bar” : “baz”, “count” : 35 }
{ “_id” : ObjectId(“4e928bf8735a86e2c6f848ed”), “apple” : 1, “banana” : 6, “peach” : 3 }
{ “_id” : ObjectId(“4e928c17735a86e2c6f848ee”), “apple” : 1, “spinach” : 4, “watermelon” : 4 }
{ “_id” : ObjectId(“4e928d8a735a86e2c6f848ef”), “bar” : “baz”, “banana” : “baz” }

需要返回有两个字段相同的文档,也就是要返回如下文档

{ “_id” : ObjectId(“4e928c17735a86e2c6f848ee”), “apple” : 1, “spinach” : 4, “watermelon” : 4 }
{ “_id” : ObjectId(“4e928d8a735a86e2c6f848ef”), “bar” : “baz”, “banana” : “baz” }

就需要使用”$where“并借助javascript来做了

db.pa_yunju_logistics.find({"$where":
 "this.erp_code!=this.code"
})

 或者

db.pa_yunju_logistics.find({"$where":function(){
 return this.erp_code!=this.code
}
})

> db.foo.find({“$where”:function(){
… for(var current in this){
…   for(var other in this){
…     if(current != other && this[current] == this[other]){
…       return true;
…     }
…   }
… }
… return false;
… }})

如果返回true,文档作为结果的一部分被返回;如果为false,则不会返回。

$where查询有以下几种写法:

> db.foo.find({“$where”:”this.x+this.y==10″})
> db.foo.find({“$where”:”function(){return this.x+this.y==10;}”})

tips:不是非常必要时,一定要避免使用”$where”查询,因为效率太低,相当的。文档在MongoDB中是以BSON格式保存的,在$where查询时,每个文档都要从BSON转换为javascript对象然后再通过”$where”中的表达式来运行。有时可以将常规查询作为前置过滤,再使用”$where”查询对结果进行调优

在Spring Boot中整合MongoDB,可以使用MongoTemplate来执行MongoDB的操作。而$unionWith是MongoDB 4.4版本中新增的集合操作符,可以用于将两个集合合并成一个集合。 在MongoTemplate中使用$unionWith操作符,可以通过以下步骤实现: 1. 定义两个集合的AggregationOperation对象: ``` AggregationOperation firstCollection = Aggregation.match(Criteria.where("field1").is("value1")); AggregationOperation secondCollection = Aggregation.match(Criteria.where("field2").is("value2")); ``` 2. 创建$unionWith操作符的AggregationOperation对象: ``` AggregationOperation unionWith = Aggregation.unionWith("second_collection", firstCollection, secondCollection); ``` 其中,"second_collection"表示要将结果合并到哪个集合中。 3. 执行聚合操作: ``` AggregationResults<Document> results = mongoTemplate.aggregate(unionWith, "first_collection", Document.class); List<Document> documents = results.getMappedResults(); ``` 其中,"first_collection"表示要执行聚合操作的集合。 完整示例代码如下: ``` AggregationOperation firstCollection = Aggregation.match(Criteria.where("field1").is("value1")); AggregationOperation secondCollection = Aggregation.match(Criteria.where("field2").is("value2")); AggregationOperation unionWith = Aggregation.unionWith("second_collection", firstCollection, secondCollection); AggregationResults<Document> results = mongoTemplate.aggregate(unionWith, "first_collection", Document.class); List<Document> documents = results.getMappedResults(); ``` 注意,使用$unionWith操作符需要MongoDB 4.4及以上版本支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值