简体   繁体   English

查找 mongodb 字段值与输入数组的某些项匹配的文档

[英]Find a mongodb Document that field value match with some item of input array

I have a string list with the dates of the days of a given week.我有一个字符串列表,其中包含给定一周中的日期。

String daysweek[] = ["10/05/2020", "11/05/2020", "12/05/2020", "13/05/2020", "14/05/2020", "15/05/2020", "16/05/2020" ]

My goal is to be able to find several documents that belong to a certain week.我的目标是能够找到属于某一周的几个文档。 The comparison field is "firstday".比较字段是“第一天”。

Follows the image of the document structure in the database:遵循数据库中文档结构的图像:

在此处输入图像描述

 Document insert = new Document().append("$elemMatch", daysweek[]);
       Document filterstar = new Document().append("id_motorista", idmotorista).append("pagamento", false).append("firstday", insert);

        coll.find(filterstar).projection(new Document().append("_id", 1).append("origem",1).append("destino", 1).append("formadepagamento", 1).append("valordaviagem",1)
                .append("notamotorista",1).append("pagamento",1).append("iniciodaviagem", 1).append("fimdaviagem",1).append("viagemcancelada", 1).append("horadaaceitacao",1)
                .append("horacancelamentomotorista", 1).append("horacancelamentousuario", 1).append("taxadecancelamento", 1).append("valordaviagemmotorista", 1).append("valordaviagemusuario", 1).append("id_acompanhamento",1)
                .append("taxaaplicativo", 1).append("taxacartao", 1).append("taxamotorista", 1)).sort(new Document().append("firstday", 1)).limit(100)
                .into(docs).addOnSuccessListener(new OnSuccessListener<List<Document>>() {
            @Override
            public void onSuccess(List<Document> documents) {}

But the search finds no documents.但是搜索没有找到任何文件。 The number of queries expected would be 35.预期的查询数为 35。

I would like to know if there is any way to find documents through a given document field, match any of the items within an arraylist.我想知道是否有任何方法可以通过给定的文档字段查找文档,匹配 arraylist 中的任何项目。

$elemMatch is used when you're querying against an array field, but in your scenario you're querying against a string field and input is an array, then you can just use $in operator. $elemMatch在您查询数组字段时使用,但在您的场景中您查询的是字符串字段并且输入是一个数组,那么您可以只使用$in运算符。

Mongo Shell Syntax: Mongo Shell 语法:

db.collection.find({firstday : {$in : ["10/05/2020", "11/05/2020", "12/05/2020", "13/05/2020", "14/05/2020", "15/05/2020", "16/05/2020"]}})

Test: mongoplayground测试: mongoplayground

The advice of @whoami works for me:D @whoami 的建议对我有用:D

So i change part of the code.所以我改变了部分代码。

I changed that:我改变了这一点:

Document insert = new Document().append("$elemMatch", daysweek[]);

to this:对此:

Document insert = new Document().append("$in", daysweek[]);

FINAL CODE:最终代码:

Document insert = new Document().append("$in", daysweek[]);
       Document filterstar = new Document().append("id_motorista", idmotorista).append("pagamento", false).append("firstday", insert);

        coll.find(filterstar).projection(new Document().append("_id", 1).append("origem",1).append("destino", 1).append("formadepagamento", 1).append("valordaviagem",1)
                .append("notamotorista",1).append("pagamento",1).append("iniciodaviagem", 1).append("fimdaviagem",1).append("viagemcancelada", 1).append("horadaaceitacao",1)
                .append("horacancelamentomotorista", 1).append("horacancelamentousuario", 1).append("taxadecancelamento", 1).append("valordaviagemmotorista", 1).append("valordaviagemusuario", 1).append("id_acompanhamento",1)
                .append("taxaaplicativo", 1).append("taxacartao", 1).append("taxamotorista", 1)).sort(new Document().append("firstday", 1)).limit(100)
                .into(docs).addOnSuccessListener(new OnSuccessListener<List<Document>>() {
            @Override
            public void onSuccess(List<Document> documents) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM