Mongo find documents where array contains x values of given array

后端 未结 1 478

I have a collection which has documents like that one. The entity field is not set in each document and has different values:

{
    \"_id\" : ObjectId(\"5388         


        
相关标签:
1条回答
  • 2021-01-16 09:39

    You can use .aggregate for this. This is probably what you're looking for:

    var y = ["Entity1", "Entity2", "Entity3", "Entity4"];
    db.col.aggregate([
        {
            $project :
            {
                _id : 1,
                name : 1,
                entity : 1,
                x : {
                    $size : {
                        $ifNull: [{$setIntersection : ["$entity", y]}, []]
                    }
                }
            } 
        },
        { $match : { x : 3 } }
    ]);
    
    0 讨论(0)
提交回复
热议问题