3

I've some 37K documents stored in Mongo that looks similar to these:

{
    "_id" : GUID,
    "Country" : "Germany",
    "TypeIds" : [47]
}


{
    "_id" : GUID,
    "Country" : "France",
    "TypeIds" : [54, 47]
}

Using the MongoDB C# driver, and based on the two example records, how can I query for the following information:

  1. All documents that have TypeIds containing 47 or 54 - should result in 2 records
  2. All documents that have TypeIds containing 47 AND 54 - should result in 1 records
  3. All documents that have TypeIds containing 54 AND a Country of 'Germany' - should result in 0 records

Thanks,
Kieron

0

1 Answer 1

3

You have class like this(i just instead of guid use GuidId.ToString()):

public class Test
        {

            public Test()
            {
                TypeIds = new List<int>();
            }

            [BsonId]
            public string Id { get; set; }

            public string Country { get; set; }

            public List<int> TypeIds { get; set; }
        }

i've inserted rows into db according above documents

  var collection = db.Database.GetCollection("items");
            var id1 = Guid.NewGuid().ToString();
            var id2 = Guid.NewGuid().ToString();
            var test = new Test() { Id = id1, Country = "Germany" };
            test.TypeIds.Add(47);
            var test2 = new Test() { Id = id2, Country = "France" };
            test2.TypeIds.Add(54);
            test2.TypeIds.Add(47);
            collection.Insert(test);
            collection.Insert(test2);

Queries:

//All documents that have TypeIds containing 47 or 54 - should result in 2 records
        var array = new List<int>() { 47, 54 };
        var condition1 = collection.FindAs<Test>(Query.In("TypeIds", BsonArray.Create(array))).ToList();

        //All documents that have TypeIds containing 54 AND a Country of 'Germany' - should result in 0 records
        var condition3 = collection.FindAs<Test>(Query.And(Query.EQ("TypeIds", 47), Query.EQ("Country", "Germany"))).ToList();

Update: I found way to do second condition:

//All documents that have TypeIds containing 47 AND 54 - should result in 1 records

     var array2 = new List<int>() { 47, 54 };
     var query = Query.All("TypeIds",BsonArray.Create(array2));

     var condition2 = collection.FindAs<Test>(query).ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

@Kieron: sure but i actually don't know how to do second condition. In mongo syntax should be something like this {"TypeIds":47,"TypeIds":54}(you can check it in mongovue at find window). But for some reason it's not work through the c# driver. But in any way you can try ;)
Can't get to mongovue.com at the moment, it seems to be down. So is this a problem with the C# driver and MongoDB does support that query, or does MongoDB simply not support that type of query?
@Kieron:Mongovue site not working for me also. I am sure that mongodb support such quries and i think that with driver also all okay, just need to play sometime with it. I do it later, because i working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.