Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

MONGO - Know if all elements of input array is in mongoDB

I have a problem with mongo query. I have the next colecction.

"id" : "TEST"
"client" : "0048948",
"contracts" : [
    {
        "contractNumberId" : "contractNumber1",
        "otherField" : "XXXXX",
        "contractFormats" : [
            {
                "formatId" : "format12"
            },
            {
                "formatId" : "format10"
            }
        ]
    },{
        "contractNumberId" : "contractNumber2",
        "otherField" : "XXXXX",
        "contractFormats" : [
            {
                "formatId" : "format12"
            },
            {
                "formatId" : "format15"
            }
        ]
    }
]

And I want to search if I have a collection in which I have all the values ??of an array of contracts or formats that are passed to me. For example if in my input I have the next contracts or formats [contractNumber1, format15] My query should return true because I found it in my database, in contracts or format, but if for example mi inputs was [contractNumbe1, format50, contract3, 10], my query should return false, because any document of my database match that all ocurrences of the input is in any document.

The query should find in contracts and formats all inputs values of input array.

Can anyone help me please?. I have tried with $elemMatch, $nor and $in, $unwind but unsatisfactory result.

Thanks to all for your time.

question from:https://stackoverflow.com/questions/66063075/mongo-know-if-all-elements-of-input-array-is-in-mongodb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...