添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

介绍语法 ifNull

Evaluates an expression and returns the value of the expression if the expression evaluates to a non-null value. If the expression evaluates to a null value, including instances of undefined values or missing fields, returns the value of the replacement expression.

db.inventory.aggregate(
         $project: {
            item: 1,
            description: { $ifNull: [ "$description", "Unspecified" ] }

description字段为project新生成的 如果description字段不为null则使用该字段如果为null则填充Unspecified字符串
输出

{ "_id" : 1, "item" : "abc1", "description" : "product 1" }
{ "_id" : 2, "item" : "abc2", "description" : "Unspecified" }
{ "_id" : 3, "item" : "xyz1", "description" : "Unspecified" } 

java spring 实现该聚合代码

//选取top30 的统计
        Aggregation aggregation = Aggregation.newAggregation(
        //保留 money way  personUserId字段默认使用字段a如果为null使用字段b
                Aggregation.project("money","way").and(ConditionalOperators.ifNull("$a").then("$b")).as("personUserId"),
                Aggregation.match(Criteria.where("way").in(1,2)),
                Aggregation.group("personUserId").sum("money").as("sumIncome"),
                Aggregation.sort(Sort.Direction.DESC, "sumIncome"),
                Aggregation.limit(30)
        List<Document> mappedResults = mongoTemplate.aggregate(aggregation, xxx.class, Document.class).getMappedResults();
        System.out.println(mappedResults);

记录一个spring使用数组表达式的方法

详细见" java – Spring Data – MongoDb聚合 ifNull" 参考链接

1.ArrayOperators.Size.lengthOfArray(
还可以使用类似
2…and((AggregationExpression) ArrayOperators.arrayOf(ConditionalOperators.ifNull(“ distriUserId").then("userId”)))这种进行操作不过这个语句是错误的只是提供思路

···
db.collection.aggregate([
{KaTeX parse error: Expected 'EOF', got '}' at position 27: …ame : "name" } }̲, {project: {
name: 1,
sent: {
KaTeX parse error: Expected '}', got 'EOF' at end of input: … "ifNull": [ “$audience”, [] ]
}
}
}
}]);
如何使用Spring数据进行上述mongo聚合?
Aggregation aggregation = Aggregation.newAggregation(
.match(Criteria.where(“name”).is(“name”))
.project(“name”) .and(ArrayOperators.Size.lengthOfArray(ConditionalOperators.ifNull(“audience”).then(Collections.emptyList()))).as(“sent”)
);
···

cond官方文档
project官方文档
Conditional 操作符
ifNull语法
java – Spring Data – MongoDb聚合$ifNull

介绍语法 ifNullEvaluates an expression and returns the value of the expression if the expression evaluates to a non-null value. If the expression evaluates to a null value, including instances of undefined values or missing fields, returns the value of the re
今天在现场的哥们发来异常,让我解决,错误信息如下: 代码如下: HTTP Status 500 – Read operation to server 192.168.1.110:20001 failed on database wpdb; nested exception is com.mongodb.MongoException$Network: Read operation to server 192.168.1.110:20001 failed on database wpdb ——————————————————————————– type Exception report
$ifNull用于判断第一个表达式是否NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值。 $ifNull表达式格式为: { $ifNull: [ <expression>, <replacement-expression-if-null> ] } 原始数据: { "_id" : 1, "item" : "abc1", ...
查询集合c中y的值为null,(仅返回y的值为null的数据,不会返回不存在的) db.c.find({“y”: {$type : 10}}) $type为10表示Null db.c.find({“y”: {“$in”: [null], “$exists”: true}}) 查询集合c中y的值不存在(不会返回y的值为null的数据) db.c.find({“y”: {$exists: false}}) 查询集合c...
等待查询数据,需要将description为null的数据在查询时候给默认值 { "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 } { "_id" : 2, "item" : "abc2", descr...
本文实例讲述了pymongo实现控制mongodb中数字字段做加法的方法。分享给大家供大家参考。具体分析如下: 这个非常实用,比如我们需要给文章做访问统计,可以设置一个数字字段:hit,然后每次点击后给改字段增加1即可 import pymongo #导入pymongo 模块 conn = pymongo.Connection() #连接本地mongodb数据库 db=conn.database #database为数据库名称 collection = db.article #article为collection名称,相当于mysql里的表名 collection.update({'_id'
映射(projection )声明用来限制所有查询匹配文档的返回字段projection以文档的形式列举结果集中要包含或者排除的字段。可以指定要包含的字段(例如:{field:1})或者指定要排除的字段(例如:{field:0})。默认_id是包含在结果集合中的,要从结果集中排除_id字段,需要在projection中指定排除_id字段({_id:0})。除了_id字段,不能在一个projection中联合使用包含和排除语意。返回匹配文档的所有字段:如果没有指定projection,find()方法返回所有匹配文档的所有字段。 代码如下:db.inventory.find( { type:
对于已存在的行,ifnull(参数0,参数1)函数对于参数0的值会进行判断。如果值为空,则返回参数1的值。 对于不存在的行,ifnull函数的结果总是null。 拿以下数据来举个例子: |id|name|age| |1 |小强 |8 | |2 |小明 |7 | |3 |小华 |null| 执行sql select ifnull(age,0) from table_name where id=3; { $ifNull: [ expression, replacement-expression-if-null ] }:用于判断第一个表达式是否null,如果为 null 则返回第二个参数的值,如果不为 null 则返回第一个参数的值。 db.getCollection('order').aggregate([ $project:{ 'sale': '$销售额', 'channel': {"$ifNull":['$渠道',