Retrieve documents where a value inside a nested array of object match a value from a given array of strings without duplicates

Viewed 54

I have an array of strings :

var ArrayOfUid = ["STK-00112","STK-00117","STK-00113","STK-00114"] 

And a list of Documents ( Products ):

        _products = database.GetCollection<Product>(settings.CollectionName);

public List<Product> Get() =>
_products.Find(produit => true).ToList();

===>

P1:
{
    "_id": {
        "$oid": "4s56sd645jez456fqdz"
    },
    "uid": "20210915-67102",
    "type": "N1",
    "fooArray": [
        {
            "uid": "STK-00117",
            "nom": "JOHN DOE T1"
        },
        {
            "uid": "STK-00114",
            "nom": "JOHN DOE T5"
        }
    ]
}

P2:
{
    "_id": {
        "$oid": "4s56sd645jez456fqdz"
    },
    "uid": "20210915-67108",
    "type": "N1",
    "fooArray": [
        {
            "uid": "STK-00117",
            "nom": "JOHN DOE T3"
        },
    ]
}



P3:
{
    "_id": {
        "$oid": "4s56sd645jez456fqdz"
    },
    "uid": "20210915-67108",
    "type": "N1",
    "fooArray": [
        {
            "uid": "STK-00115",
            "nom": "JOHN DOE T4"
        },
        {
            "uid": "STK-00117",
            "nom": "JOHN DOE T10"
        },
        {
            "uid": "STK-00113",
            "nom": "JOHN DOE T18"
        },
    ]
}

P4:


{
    "_id": {
        "$oid": "4s56sd645jez456fqdz"
    },
    "uid": "20210915-67108",
    "type": "N1",
    "fooArray": [
        {
            "uid": "STK-00113",
            "nom": "JOHN DOE T15"
        },
        {
            "uid": "STK-00112",
            "nom": "JOHN DOE T16"
        },
        {
            "uid": "STK-00111",
            "nom": "JOHN DOE T17"
        },
    ]
}

P5 : 
{
    "_id": {
        "$oid": "4s56sd645jez456fqdz"
    },
    "uid": "20210915-67108",
    "type": "N1",
    "fooArray": [
        {
            "uid": "STK-00999",
            "nom": "JOHN DOE T99"
        },
    ]
}

I Want to get all the products where fooArray.uid in ArrayOfUid

So in the end I get the list of products like this without duplicates

on MongoDb Side query :

db.collection.find({
                "fooArray.uid": {
    $in: [
      "STK-00113",
      "STK-00117",
      "STK-00113",
      "STK-00114"
                  ]
  }
            })

This will achieve the desired result, Is there a way to do it with LinQ on ASP.NET ?

[

    {
        "_id": {
            "$oid": "4s56sd645jez456fqdz"
        },
        "uid": "20210915-67102",
        "type": "N1",
        "fooArray": [
            {
                "uid": "STK-00117",
                "nom": "JOHN DOE T1"
            },
            {
                "uid": "STK-00114",
                "nom": "JOHN DOE T5"
            }
        ]
    }
    

    {
        "_id": {
            "$oid": "4s56sd645jez456fqdz"
        },
        "uid": "20210915-67108",
        "type": "N1",
        "fooArray": [
            {
                "uid": "STK-00117",
                "nom": "JOHN DOE T3"
            },
        ]
    }
    
    

    {
        "_id": {
            "$oid": "4s56sd645jez456fqdz"
        },
        "uid": "20210915-67108",
        "type": "N1",
        "fooArray": [
            {
                "uid": "STK-00115",
                "nom": "JOHN DOE T4"
            },
            {
                "uid": "STK-00117",
                "nom": "JOHN DOE T10"
            },
            {
                "uid": "STK-00113",
                "nom": "JOHN DOE T18"
            },
        ]
    }
    
 
    
    
    {
        "_id": {
            "$oid": "4s56sd645jez456fqdz"
        },
        "uid": "20210915-67108",
        "type": "N1",
        "fooArray": [
            {
                "uid": "STK-00113",
                "nom": "JOHN DOE T15"
            },
            {
                "uid": "STK-00112",
                "nom": "JOHN DOE T16"
            },
            {
                "uid": "STK-00111",
                "nom": "JOHN DOE T17"
            },
        ]
    }


]

So I want to enter the list of string and go thorough it and return a list of documents that match one value of my array of string.

What I'm trying to do right now , but couldn't manage to make it work

public IEnumerable<Product> GetFromSites(List<string> ArrayOfUid)
{
    var client = new MongoClient(new MongoUrl("mongodb://localhost:27017"));
    var db = client.GetDatabase("Database");
    var productsCollection = db.GetCollection<Product>("Products");

   // I need to rework this filter
    var filter = Builders<Produit>.Filter
        .In(p => p.fooArray.uid, ArrayOfUid);

        //List<fooArray>' does not contain a definition for 'uid' and no accessible extension method 'uid' accepting a first argument of type 'List<fooArray>' could be found (are you missing a using directive or an assembly reference?) 

    var products = _produits
        .Find(filter)
        .ToEnumerable();

    return products;
}
0 Answers
Related