MongoDB Java驱动程序传递未定义的值 [英] MongoDB java driver passing undefined as value

查看:53
本文介绍了MongoDB Java驱动程序传递未定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是mongoDB聚合,它正在过滤mongodb集合项中的测试数组.

I've below mongoDB aggregation which is filtering tests array in my mongodb collection item.

样品采集项目:{,... tests:[{},{"someField":"yesIamHere"}]}

Sample collection item : {,...tests:[ {} , {"someField":"yesIamHere"} ] }

以下查询效果很好,并且仅返回包含 someField

Below query worked well and returned only tests collection which contains someField

db.getCollection('yourcollection')
.aggregate([
{"$match": {"tests.someField": {"$exists": true}}},
{ $project:{"tests": {"$filter": {"input": "$tests", "as": "item", 
    "cond": {"$ne": ["$$item.someField", undefined]}}}}
},
])

但是,

使用Java BasicDBObject时,将未定义" 作为字符串而不是JS undefined

While using java BasicDBObject is taking "undefined" as string not JS undefined

    BasicDBObject projectionFilterInput=new BasicDBObject("input","$tests")
        .append("as", "item")
        .append("cond",new BasicDBObject("$ne", Arrays.asList("$$item.someField","undefined")));

因此,这解释了"cond":{"$ ne":[["$$ item.vidaptorCode","undefined"]]}}} "未定义" ,而不是未定义.因此,这不会按预期过滤项目.

So, this interprets "cond": {"$ne": ["$$item.vidaptorCode", "undefined"]}}}} "undefined" not undefined. So, this doesn't filter the items as intended.

在mongodb java驱动程序库中是否为此特定的 undefined 值定义了任何常量?这是主要问题.

Is there any constant defined for this specific undefined value in mongodb java driver base ? This is the main question.

谁好奇...

我为什么不使用ODM?

实际上,我们确实将 Spring数据用于MongoDB ,但它不支持此聚合 cond .

Actually we do use Spring Data for MongoDB, but it doesn't support this aggregation cond.

    MatchOperation matchStage = Aggregation.match(new Criteria("tests.someField").exists(true));        
    ProjectionOperation projection = Aggregation.project("tests");  

Aggregation aggregation 
      = Aggregation.newAggregation(matchStage, projection);
    
    AggregationResults<LabConfiguration> output 
      = mongoTemplate.aggregate(aggregation, "yourcollection", YourClass.class);

Morphia ODM

我喜欢Morphia流畅的语法,但是它们使用的注释与Spring Data Mongo不同,并且其依赖的MongoDB库也不同.简而言之,两个ODM不能一起工作.最大的问题是您需要实现 BasicDAO< C,K> 的存储库实现,它不是很实用,它是面向mongo的,而Spring Data Mongo在 MongoRepository< C,K>方面做得很好.

I liked Morphia fluent syntax however they're using different annotations than Spring Data Mongo and its dependent MongoDB libs are different. In short, two ODM don't work together. The biggest problem is for repository implementation you need to implement BasicDAO<C,K> and it's not very practical, it's mongo oriented and Spring Data Mongo does great job with MongoRepository<C,K>

    Projections filterProjection = projection(
            "tests",
            expression(
                    "$filter",
                    new BasicDBObject("input","$tests")
                    .append("as", "item")
                    .append("cond",new BasicDBObject("$ne", Arrays.asList("$$item.someField","undefined")))
            )
    );

因此,对于这个问题,我最终使用了Mongo驱动程序的基本语法,这就是为什么我需要将 undefined 传递给BasiDBObject而不是用双引号覆盖的字符串的原因.

Hence, I ended up with Mongo driver base syntax for this problem that's why I need to pass undefined to BasiDBObject but not as a string covered by double quotes.

我也乐于听取您的总体建议.我们现在拥有的是 QueryDSL MongoDB的Spring数据.

I'm also open to hear your overall advices. What we have now is QueryDSL and Spring Data for MongoDB.

推荐答案

正如我的OP中其他人所评论的,替代方法是使用 $ ifNull .

As commented by others in my OP, the alternative is using $ifNull.

实际上,我期望使用 {extists:true} ,但这不是有效的 aggregation cond 运算符,或者类似 $ ifNotNull 这样的运算符很好,但是使用 $ ifNull 也可以实现.

Actually, I was expecting using an {extists:true} but it's not a valid aggregation cond operator or something like $ifNotNull would be nice, however it's also achievable with $ifNull.

您知道投影适用于0和1,即 {name:1,_id:0}

As you know projection works with 0 and 1, i.e. {name:1,_id:0}

所以,我决定在 $ ifNull 起作用时返回 0

So, I decided to return 0 when $ifNull and it worked!

Java MongoDB核心驱动程序方式

BasicDBObject projectionFilterInput=new BasicDBObject("input","$tests")
.append("as", "item")
.append("cond",new BasicDBObject("$ifNull",Arrays.asList("$$item.someField",0)));

这篇关于MongoDB Java驱动程序传递未定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆